Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

Comments on A chunk of symbols is a calculation [released]

Post

A chunk of symbols is a calculation [released]

+2
−0

Challenge

Create a program that takes input of a string and outputs an integer using the following calculation system:

  • The string can only contain +, -, *, /, ^, ! (or a different character; ! is used here for demonstration), (, and ).
  • In this system, a character shows the value of $1$. This demonstrates the value of an integer with the system. Examples are +++++, which is equal to + 5, and ***, which is * 3.
  • Here are the operations of each symbol:
    • + is addition and for assigning a beginning positive integer. (+++++++++ = + 9)
    • - is subtraction and for assigning a beginning negative integer. (------------- = -13/- 13)
    • * is multiplication. (** = * 2)
    • / is division. (////// = / 6)
    • ^ is exponentiation/ (^^^ = ^ 3)
    • ! or a different character (I'm using ! for demonstration) is for assigning positive integers if we want to add positive integers together, or to simply separate positive integers from each other.
    • ( and ) are for grouping calculations, and can also be used for multiplying a number into an equation. The only way to interact with the grouped equations is if no numbers precede them. !!!+++++(----***)// = 3 + 4(-4 * 3) / 2)
  • The output must be an integer, being the result of the whole equation.
  • The whole system should follow this non-standard model of PEMDAS.
  • Being a code-golf challenge, the shortest program of each language wins.

Testcases

  • +++++ is + 5 which returns 5.
  • ---** is -3 * 2 which returns -6.
  • ++(----//+++++!!!**) is 2(-4 / 2 + 5 + 3 * 2) which returns 18 (2(-2 + 5 + 6) => 2(9)).
  • --^^^ is -2 ^ 3 which returns -8.
  • An empty input or () is 0 which returns 0.
  • (+-)/(+-) is (1 - 1) / 1 (1 - 1) is indeterminate, but for the sake of this challenge, it returns 0.
  • (++++++++******)//((+++***)+++) is (8 * 6) / 2((3 * 3) + 3) which returns 2 (48 / 2(9 + 3) => 48 / 24).

Error Handling

Everything in this section is undefined behavior. Please DON'T do these when inputting.

  • A character that's not supposed to be there.
  • A beginner number is assigned NOT by +, -, ! (or a different character), or (.
  • An open parentheses doesn't have a paired closed parentheses.
  • A whitespace character in the string (tabs, spaces, etc.).
  • Characters that aren't part of the English alphabet. ñ, and θ are examples.
  • An equation like !/(+-) or 1 / 1(1-1) leads to the division by zero error. Refrain from doing such equations in the input. Only 0 / 0 is an exception here, which returns 0.

Suggestions

  • Any way to improve this?
  • Anything unclear?
  • Are these the right tags?
  • Any more errors to handle?
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

5 comment threads

What is an exclamation mark? (4 comments)
What do you mean by "(or something else)"? (2 comments)
Unary operators other than - and +? (3 comments)
Are function entries allowed? (2 comments)
Division (2 comments)
Unary operators other than - and +?
user‭ wrote over 3 years ago

What do ^^^ (^ 3) and // (/ 2) return? As far as I know, they aren't used as unary operators in math. Is this simply undefined behavior?

General Sebast1an‭ wrote over 3 years ago · edited over 3 years ago

user‭ No, actually. It's basically the implementation of the operation from the preceding number. For example, // is basically x / 2. And I mentioned you CAN'T use symbols other than +, -, ! and ( at the very beginning of the input because it will return something to show it's invalid.

user‭ wrote over 3 years ago

Thanks for clarifying that in the draft, but I don't like the error handling part. It doesn't add anything to the challenge and is just cumbersome. Much better to simply say that everything that's currently in the error handling section is just undefined behavior.