Challenge : creating new operators | SoloLearn: Learn to code for FREE!

+15

Challenge : creating new operators

In this challenge, you'll have to create a new operator : the exponent operator (so languages who already have it are not allowed). The syntax, when using this operator, should feel natural. The easy version is with the '^' symbol. The hard one is with the '**' symbol. Your operator should be usable like that : 2^3 or 3.5**4.2 Good luck :) BONUS : For those who find it too easy, try to make the +++ operator, to do two time the ++ operator (+++i or i+++ are equivalent to ++i;++i or i++;i++)

8/9/2017 5:36:39 PM

Baptiste E. Prunier

27 Answers

New Answer

+2

here are solutions https://code.sololearn.com/cnoXp5H1a4gL/?ref=app

+11

https://code.sololearn.com/cMAW3cx2ZfNZ/?ref=app Is this what you mean?

+7

@Baptiste: Javascript already have a ** operator only since last ES6 specs, which are not already fully crossbrowser supported ^^ (wow: ^^ is power of power in this context :D)

+6

It was quite interesting, I did some research and I came across this: https://github.com/klmr/named-operator Which demonstrates how to make your own operators in C++ like this: https://code.sololearn.com/c23FwvpzbVtc/?ref=app You learn something new every day ^^

+4

To make it clear, I did it so that it wouldn't be easy on purpose :p @Pixie, yes for the first part it is right ! @Denis wooo ... just wooo .... I am glad I made this challenge just for your answer ! I will take some screen shots of the code to study it in details !

+3

Nice try @ysraelcon but you do not really make a new operator directly usable in the JavaScript code and JavaScript already have a ** operator :/

+3

good challenge

+3

Java doesn't support operator overloading 😒 (feeling like a grounded naughty kid)

+3

I wrote the exact same code for Python. :( I would've preferred using the old Infix () function as that used to be the only possible way to make semi-kinda custom operators. Otherwise Python is a convenient, but very heavily restrictive language. x.x

+2

hm. that makes it hard since every language I know has already an exponent operator.

+2

This sounds difficult since majority of languages I'm even aware of personally have exponents incorporated into them. Are we allowed to just override default behavior of exponents the languages we're already using?

+2

No @VcC as you used an already existing exponent operator ^^ Plus ** is hard in languages which do not have a ** operator (like Cpp)

+2

@Sapphire, I should have said that if ** exist, overloading ** would have been the same as ^ for other languages I am sad nobody manage to do the bonus, because neither do I :p Here is what I did (did it just after posting the thread so I didn't see your answers before) https://code.sololearn.com/cp3M2lJpklrH/#cpp

+1

Yeah @visph, for languages who have already **, you can do the ^ part (and +++ of course)

+1

Nice challenge, I will keep this in mind. But not now, I'm not enough experienced T_T.

+1

No @VcC and @Daniil, you are doing functions, not operators

+1

Yeah, or at least nearly @VcC. As I said before, if the language already have the ** operator as an exponent, you'll have to use ^ instead

+1

Nice work @Marek Wawrzos ! The two first points are clean and working ! nice ! The bonus point is not in your code, what you did is just overloading the ++ operator, so we can only have an even number of '+', we can not have '+++' like asked

+1

You are right @Baptiste E. Prunier ! I have made a fix. Now it works with any number of ++'es

0

doing a simply try in https://code.sololearn.com/WPO79PKx8Mvy/?ref=app 😓