Skip to content

Commit 7f92158

Browse files
committed
some other nice exercise
1 parent 541f74b commit 7f92158

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Some people have a habit of <s>lecturing</s> speaking rather quickly, and it’d be nice to slow them down, a la YouTube’s 0.75 playback speed, or even by having them pause between words.
2+
3+
In a file called `playback.py`, implement a program in Python that prompts the user for input and then outputs that same input, replacing each space with `...` (i.e., three periods).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def main():
2+
sentence = input("Input: ")
3+
print(playback(sentence))
4+
5+
6+
def playback(s):
7+
return s.strip().replace(" ", "...")
8+
9+
10+
if __name__ == "__main__":
11+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Tip Calculator
2+
3+
In the United States, it’s customary to leave a tip for your server after dining in a restaurant, typically an amount equal to 15% or more of your meal’s cost. Not to worry, though, we’ve written a tip calculator for you, below!
4+
5+
```python
6+
def main():
7+
dollars = dollars_to_float(input("How much was the meal? "))
8+
percent = percent_to_float(input("What percentage would you like to tip? "))
9+
tip = dollars * percent
10+
print(f"Leave ${tip:.2f}")
11+
12+
13+
def dollars_to_float(d):
14+
# TODO
15+
16+
17+
def percent_to_float(p):
18+
# TODO
19+
20+
21+
main()
22+
```
23+
24+
Well, we’ve written *most* of a tip calculator for you. Unfortunately, we didn’t have time to implement two functions:
25+
26+
* `dollars_to_float`, which should accept a `str` as input (formatted as `$##.##`, wherein each `#` is a decimal digit), remove the leading `$`, and return the amount as a `float`. For instance, given `$50.00` as input, it should return `50.0`.
27+
* `percent_to_float`, which should accept a str as input (formatted as `##%`, wherein each `#` is a decimal digit), remove the trailing `%`, and return the percentage as a `float`. For instance, given `15%` as input, it should return `0.15`.
28+
Assume that the user will input values in the expected formats.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def main():
2+
dollars = dollars_to_float(input("How much was the meal? "))
3+
percent = percent_to_float(input("What percentage would you like to tip? "))
4+
tip = dollars * percent
5+
print(f"Leave ${tip:.2f}")
6+
7+
8+
# $##.## format
9+
def dollars_to_float(d):
10+
#return float(d.replace("$", ""))
11+
return float(d[1:]) # from the second character to the end
12+
13+
14+
# ##% format
15+
def percent_to_float(p):
16+
#return float(p.replace("%", "")) / 100
17+
return float(p[:-1]) / 100 # from the beginning to the second to last character
18+
19+
20+
if __name__ == "__main__":
21+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Grocery List
2+
3+
Suppose that you’re in the habit of making a list of items you need from the grocery store.
4+
5+
In a file called `grocery.py`, implement a program that prompts the user for items, one per line, until the user inputs control-d (which is a common way of ending one’s input to a program). Then output the user’s grocery list in all uppercase, sorted alphabetically by item, prefixing each line with the number of times the user inputted that item. No need to pluralize the items. Treat the user’s input case-insensitively.
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def main():
2+
items = {}
3+
while True:
4+
try:
5+
item = get_item()
6+
except EOFError: # Ctrl-D
7+
print(" ") # otherwise the prompt is on Item: ^D
8+
break
9+
if item == "":
10+
continue
11+
items[item] = items.get(item, 0) + 1
12+
13+
for item in sorted(items):
14+
print(items[item], item)
15+
16+
17+
18+
def get_item():
19+
return input("Item: ").strip().upper()
20+
21+
22+
if __name__ == "__main__":
23+
main()

0 commit comments

Comments
 (0)