Skip to content

Instantly share code, notes, and snippets.

@slothelle
Last active December 14, 2015 16:37
Show Gist options
  • Save slothelle/d408d2ec8a8b174914f7 to your computer and use it in GitHub Desktop.
Save slothelle/d408d2ec8a8b174914f7 to your computer and use it in GitHub Desktop.
Mostly non-math based introductory programming challenges.

Knowledge required

  • Conditional statements (if, etc)
  • Loops (while, etc)
  • Lists ([1, 2, 3])
  • Priting to standard out (affectionately known as STDOUT)
  • The difference between strings and integers

Additional information

All of your code should be able to handle failure cases. Assume that your user is the worst at following instructions and won't ever give you the right kind of thing in your argument. Return helpful error messages whenever you can.

Calculate the total of a list

Write a method total which takes a list of numbers as its input and returns their total (sum).

Additional requirements:

  1. Your code should be able to reject things that aren't numbers
  2. Your code should be able to handle negative numbers and decimals
  3. Your code should return a number

Calculate a letter grade

Create a procedure get_grade that accepts an average in the class and returns the letter grade as a String.

It should only return one of 'A', 'B', 'C', etc. Don't worry about + and - grades.

Count between

Write a procedure count_between which takes three arguments as input:

  1. A list of integers
  2. An integer lower bound
  3. An integer upper bound

count_between should return the number of integers in the list between the two bounds, including the bounds.

It should return 0 if the list is empty.

It should return 2 if there are no numbers between the bounds.

Example:

count_between([1, 2, 4, 5], 2, 5) # => 3
count_between([1, 2, 4, 5], 1, 5) # => 5
count_between([1, 2, 4, 5], 4, 5) # => 2
count_between([], 4, 5) # => 0

Calculate a letter grade, advanced

Using your get_grade procedure from earlier, modify it to accept a list of intergers as an argument calculate a student's overall grade.

Each score in the list should be between 0 and 100, where 100 is the max score.

Compute the average score and return the letter grade as a String, i.e., 'A', 'B', 'C', 'D', 'E', or 'F'.

Example:

get_grade([93, 100, 95]) # => A

Print a pretty triangle!

Create a procedure called print_triangle that accepts an integer as an argument and prints out a right triangle of rows rows consisting of * characters:

Example:

print_triangle(4)

Should print:

*
**
***
****

Separate numbers by commas

Write a procedure separate_comma which takes an integer as its input and returns a comma-separated integer as a string.

separate_comma(1000)    # => "1,000"
separate_comma(1000000) # => "1,000,000"
separate_comma(0)       # => "0"
separate_comma(100)     # => "100"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment