9/11/22

Problem 2 - The game of Nims / Stones

 In this game, two players sit in front of a pile of 100 stones. They take turns, each removing between 1 and 5 stones (assuming there are at least 5 stones left in the pile). The person who removes the last stone(s) wins.


Write a program to play this game. This may seem tricky, so break it down into parts. Like many programs, we have to use nested loops (one loop inside another).


In the outermost loop, we want to keep playing until we are out of stones.


Inside that, we want to keep alternating players. You have the option of either writing two blocks of code, or keeping a variable that tracks the current player. The second way is slightly trickier since we haven't learned lists yet, but it's definitely do-able!


Finally, we might want to have an innermost loop that checks if the user's input is valid. Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are no, we should tell the user and re-ask them the question.


So, the basic outline of the program should be something like this:


TOTAL = 100


MAX = 5


pile = TOTAL # all stones are in the pile to start


while [pile is not empty]:


while [player 1's answer is not valid]:


[ask player 1]


[check player 1's input... is it valid?]


[same as above for player 2]


Note how the important numbers 100 and 5 are stored in a single variable at the top. This is good practice -- it allows you to easily change the constants of a program. For example, for testing, you may want to start with only 15 or 20 stones.


Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their answer is not valid, BUT we want to make sure we ask them at least ONCE. So, for example, we will want to keep a variable that tracks whether their answer is valid, and set it to False initially.


When you're finished, test each other's programs by playing them!


Problem 2 - Cafe menu

 Write a program that first displays a simple cafe menu (see example below), asks the user to enter the number of a choice, and either prints the appropriate action OR prints an error message that their choice was not valid.


Example output:


1. Soup and salad


2. Pasta with meat sauce


3. Chef's special


Which number would you like to order? 2


One Pasta with meat sauce coming right up!


Another example output:


1. Soup and salad


2. Pasta with meat sauce


3. Chef's special


Which number would you like to order? 5


Sorry, that is not a valid choice

Registration System

 You are making a registration form for a website.

The form has a name field, which should be more than 3 characters long.

Any name that has less than 4 characters is invalid.

Complete the code to take the name as input, and raise an exception if the name is invalid, outputting "Invalid Name". Output "Account Created" if the name is valid.

Sample Input

abc

Sample Output

Invalid Name

Shooting Game

 You are creating a shooting game!

The game has two types of enemies, aliens and monsters. You shoot the aliens using your laser, and monsters using your gun.

Each hit decreases the lives of the enemies by 1.

The given code declares a generic Enemy class, as well as the Alien and Monster classes, with their corresponding lives count.

It also defines the hit() method for the Enemy class.

You need to do the following to complete the program:

1. Inherit the Alien and Monster classes from the Enemy class.

2. Complete the while loop that continuously takes the weapon of choice from user input and call the corresponding object's hit() method.

Sample Input

laser

laser

gun

exit

Sample Output

Alien has 4 lives

Alien has 3 lives

Monster has 2 lives

9/9/22

Spelling Backwards

 Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line.

Sample Input

HELLO

Sample Output

O

L

L

E

H

9/8/22

Letter Counter

 Given a string as input, you need to output how many times each letter appears in the string.

You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values.

Create a program to take a string as input and output a dictionary, which represents the letter count.

Sample Input

hello

Sample Output

{'h': 1, 'e': 1, 'l': 2, 'o': 1}

Fruit Bowl

 You have a bowl on your counter with an even number of pieces of fruit in it. Half of them are bananas, and the other half are apples. You need 3 apples to make a pie.

Task

Your task is to evaluate the total number of pies that you can make with the apples that are in your bowl given to total amount of fruit in the bowl.

Input Format

An integer that represents the total amount of fruit in the bowl.

Output Format

An integer representing the total number of whole apple pies that you can make.

Sample Input

26

Sample Output

4

Popsicles

 You have a box of popsicles and you want to give them all away to a group of brothers and sisters. If you have enough left in the box to give them each an even amount you should go for it! If not, they will fight over them, and you should eat them yourself later.

Task

Given the number of siblings that you are giving popsicles to, determine if you can give them each an even amount or if you shouldn't mention the popsicles at all.

Input Format

Two integer values, the first one represents the number of siblings, and the second one represents the number of popsicles that you have left in the box.

Output Format

A string that says 'give away' if you are giving them away, or 'eat them yourself' if you will be eating them yourself.

Sample Input

3 9

Sample Output

give away


Contact Search

 You are given a list of contacts, where each contact is represented by a tuple, with the name and age of the contact.

Complete the program to get a string as input, search for the name in the list of contacts and output the age of the contact in the format presented below:

Sample Input

John

Sample Output

John is 31

National Economic Freedom

 You are working on data that represents the economic freedom rank by country.

Each country name and rank are stored in a dictionary, with the key being the country name.

Complete the program to take the country name as input and output its corresponding economic freedom rank.

In case the provided country name is not present in the data, output "Not found".

9/7/22

Car data

 You are working at a car dealership and store the car data in a dictionary:

car = {

    'brand': 'BMW',

    'year': 2018,

    'color': 'red'

}

Your program needs to take the key as input and output the corresponding value.

Sample Input

year

Sample Output

2018

Search Engine

 You’re working on a search engine. Watch your back Google!

The given code takes a text and a word as input and passes them to a function called search().

The search() function should return "Word found" if the word is present in the text, or "Word not found", if it’s not.


Sample Input

"This is awesome"

"awesome"

Sample Output

Word found


How many letters

 Write a function that takes a string and a letter as its arguments and returns the count of the letter in the string.

Sample Input

hello, how are you?

o

Sample Output

3

Explanation: The letter o appears 3 times in the given text.

From feet to inches

 You need to make a function that converts a foot value to inches.

1 foot has 12 inches.

Define a convert() function, that takes the foot value as argument and outputs the inches value.

Shouting text

 Your friend is sending you a text message, however his caps lock is broken and the whole message is in uppercase.

Noone likes being shouted at, plus uppercase text is hard to read, so you decide to write a program to convert the text to lowercase and output it.

Take a string as input and output it in lowercase.

Analyze to realize

 You’re analyzing a data set and need to remove the outliers (the smallest and largest values.

The data is stored in a list).

Complete the code to remove the smallest and largest elements from the list and output the sum of the remaining numbers.

Sum of Consecutive Numbers

 No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers.

Let’s save some time by creating a program to do the calculation for you!

Take a number N as input and output the sum of all numbers from 1 to N (including N).

Sample Input

100

Sample Output

5050

Explanation: The sum of all numbers from 1 to 100 is equal to 5050.

9/6/22

Flip the string

 Reversing a string is a common task during coding interviews.

Given a string as input, output its reverse.

Sample Input

hello world

Sample Output

dlrow olleh

Nearest Bathroom

 A group of buildings have restrooms on every 5th floor.

For example, a building with 12 floors has restrooms on the 5th and 10th floors.

Create a program that takes the total number of floors as input and outputs the floors that have restrooms.

Sample Input

23

Sample Output

5

10

15

20

Let's go shopping

 You’re making a shopping cart program.

The shopping cart is declared as a list of prices, and you need to add functionality to apply a discount and output the total price.

Take the discount percentage as input, calculate and output the total price for the shopping cart.

Just say it

 You’re making a voice recognition system.

The supported commands are stored in a list.

Complete the program to take a command as input, check if it’s supported and output "OK", if it is, and "Not supported", if not.

Sample Input

Lights Off

Sample Output

OK


9/5/22

Name please

 You need to take the first and last name of a person as input and output the initials (first letters of the first and last name).

Sample Input

James

Smith

Sample Output

J.S.

Where's my seat?

 The seats in your ticketing program are stored in a 2D list. Each seat is assigned a letter code.

Complete the program to take the seat row and column as input and output the corresponding code from the list (row and column indices start from 0).

Sample Input

3

2

Sample Output

l

BMI Calculator

 Tracking your BMI is a useful way of checking if you’re maintaining a healthy weight. It’s calculated using a person's weight and height, using this formula: weight / height²

The resulting number indicates one of the following categories:

Underweight = less than 18.5

Normal = more or equal to 18.5 and less than 25

Overweight = more or equal to 25 and less than 30

Obesity = 30 or more

Let’s make finding out your BMI quicker and easier, by creating a program that takes a person's weight and height as input and outputs the corresponding BMI category.

Sample Input

85

1.9

Sample Output

Normal

9/4/22

Pull the trigger

 You are making a game! The player tries to shoot an object and can hit or miss it.

The player starts with 100 points, with a hit adding 10 points to the player’s score, and a miss deducting 20 points.

Your program needs to take 4 action results as input ("hit" or "miss"), calculate and output the player’s remaining points.

Sample Input

hit

hit

miss

hit

Sample Output

110

Explanation: 3 hits add 30 points, one miss deducts 20, making the total points equal to 110.


24k magic

 Now that we know how to combine multiple conditions, let’s improve our gold purity checker program and output the corresponding purity level in Karats!

Here is the purity chart we’ll be using:

24K – 99.9%

22K – 91.7%

20K – 83.3%

18K – 75.0%

If the percentage is between 75 and 83.3, the gold is considered to be 18K.

If it's between 83.3 and 91.7 - then it’s 20K, and so on.

Given the percentage as input, output the corresponding Karat value, including the letter K.

Sample Input:

92.4

Sample Output:

22K

Leap Year

 You need to make a program to take a year as input and output "Leap year" if it’s a leap year, and "Not a leap year", if it’s not.

To check whether a year is a leap year or not, you need to check the following:

1) If the year is evenly divisible by 4, go to step 2. Otherwise, the year is NOT leap year.

2) If the year is evenly divisible by 100, go to step 3. Otherwise, the year is a leap year.

3) If the year is evenly divisible by 400, the year is a leap year. Otherwise, it is not a leap year.

Sample Input

2000

Sample Output

Leap year

Use the modulo operator % to check if the year is evenly divisible by a number.

Pure Gold

 You’re making a gold purity checker, that should accept only 22K or 24K gold. Only the good stuff!

22K gold has 91.7% or more gold in it, while 24K corresponds to 99.9% and above purity.

Given the % of gold purity as input, you should output "Accepted" only if it corresponds to 22 or 24K.

Sample Input:

93.4

Sample Output:

Accepted

Tip Calculator

 When you go out to eat, you always tip 20% of the bill amount. But who’s got the time to calculate the right tip amount every time? Not you that’s for sure! You’re making a program to calculate tips and save some time.

Your program needs to take the bill amount as input and output the tip as a float.

Sample Input

50

Sample Output

10.0

9/3/22

Identity Card

 Now that we know how to take user input, let's improve our contact card program that we previously made using variables.

Change the given code to take the name and age from user input and use them in the output.

Get Notified

 You’re working on a notification system and need to make the notification text eye-catching.

Write a program that takes the text as input and outputs it by adding 3 stars at the beginning and the end.

Sample Input

this is awesome

Sample Output

*** this is awesome ***

Contact Card

 You’re making a contact management system.

The given program declares two variables, name and age.

Complete the code to output "name is age years old", where name and age are the declared variable values.

Quotient & Remainder

 Random task! You need to calculate the number of hours in 888 minutes.

Your program needs to output the number of hours and then the number of remaining minutes, on separate lines.

For example, 72 minutes are equal to 1 hour and 12 minutes, so your program would output:

1

12

Exponentiation

 Did you know that there are more bacteria cells in your body than cells that make up your body? Weird!

A bacteria culture starts with 500 bacteria and doubles in size every hour.

Which means, after 1 hour the number of bacteria is 1000, after 2 hours - 2000, and so on.

Let’s calculate and output the number of bacteria that will be in the culture after 24 hours.

Simple Operations

 Ever wondered how many seconds are there in a month (30 days)?

Write a program to calculate and output the answer.

Leaderboard

 You need to make a program for a leaderboard.

The program needs to output the numbers 1 to 9, each on a separate line, followed by a dot:

1.

2.

3.

...

Flight Time

 You need to calculate the flight time of an upcoming trip. You are flying from LA to Sydney, covering a distance of 7425 miles, the plane flies at an average speed of 550 miles an hour.

Calculate and output the total flight time in hours.

Hint

The result should be a float.

Blog Archive