Coder Social home page Coder Social logo

exercise03's Introduction

Exercise 03: Interfaces, main loops revisited

Introduction

As we saw before, a main loop looks like this:

do_setup()
while exit_condition_not_reached:
    input = consume_input()
    output = evaluate_input(input)
    print output

When we consume input from the keyboard, this is a special type of main loop called a 'REPL', a read-eval-print-loop.

In exercise 1, we built a very simple REPL that asked for a single number from the user.

In exercise 2, we provided a REPL for you that implemented a basic calculator.

In this exercise, you will re-implement the same REPL from scratch. To do this, you will need to tokenize a string.

Tokenizing a string is the process of taking a string and breaking it up into its constituent parts as a list. Consider the following string:

ocean_animals = "shark,squid,tuna,flounder"

A natural tokenization would be the following array:

["shark", "squid", "tuna", "flounder"]

To do this, we use the string split method:

ocean_animals.split(",")
    => ["shark", "squid", "tuna", "flounder"]

When we do this, we say we've tokenized the original string on commas.

Now consider the following string as read from the keyboard:

input = "pow 3 5"

If we tokenize on spaces, we get the following list:

tokens = input.split(" ")
# tokens = ["pow", "3", "5"]

Now, we can take the first token, token[0], and make a decision about what to do with the remaining tokens. For example:

if tokens[0] is equal to "pow":
    call the power function with the other two tokens

The psuedocode for our REPL will look like this:

# No setup
repeat forever:
    read input
    tokenize input
    if the first token is 'q', quit
    otherwise decide which math function to call based on the tokens we read
Do These First:
Concepts required:
  • functions
  • arithmetic
  • return values
  • string parsing
  • conditionals

Description

Implement a REPL for a calculator in a file named 'calculator.py'. Your calculator will use the interface from the previous exercise:

In the file arithmetic.py, these function signatures are required

add(int, int) -> int
Returns the sum of the two input integers

subtract(int, int) -> int
Returns the second number subtracted from the first

multiply(int, int) -> int
Multiplies the two inputs together

divide(int, int) -> float
Divides the first input by the second, returning a floating point

square(int) -> int
Returns the square of the input

cube(int) -> int
Returns the cube of the input

power(int, int) -> int
Raises the first integer to the power of the second integer and returns the value.

mod(int, int) -> int
Returns the remainder when the first integer is divided by the second integer.

A sample session of the calculator looks like this:

Meringue:math chriszf$ python calculator.py
> + 1 2
3
> - 10 5
5
> * 2 3
6
> / 6 2
3.000000
> square 2
4
> cube 3
27
> pow 2 5
32
> mod 10 3
1
> q
Meringue:math chriszf$

We have provided a sample arithmetic.py with dummy stubs that do the wrong thing. When you've completed your REPL, copy your completed arithmetic.py from the previous exercise to the current directory and replace our stubs.

exercise03's People

Contributors

hackbrightsalesforcegithub avatar katereese avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.