|
CS 116 Tutorial 3: Strings and Input/Output
Reminders: Assignment 3 is due Wednesday February 5th at 10am
- Write a function
closest_integer that has no arguments, but instead reads
in a floating point number from console input with a prompt "What's the number?" , and returns the closest
integer to that number. This function rounds ties up, so that
closest_integer(0.5) is 1, while closest_integer(-0.5) is 0.
DO NOT use round in your solution.
Write a function create_date that consumes nothing, but takes keyboard
input. The program has three prompts: "Enter the year: " ,
"Enter the month: " and "Enter the day: " . The function then returns a
date in the form "dd/mm/yyyy" , where dd is a 2 digit number
(between 01 and 31, depending on the month), mm is a 2 digit number
(between 01 and 12) and yyyy is a 4 digit number.
For example,
create_date()
Enter the year: 1996
Enter the month: 06
Enter the day: 17
=> "17/06/1996"
Use string methods and string formatting (using {}) to complete the question
Write a function fill_the_string that consumes a non-empty string s
and a positive integer n , and returns an n-letter string that consists
of copies of s , where the last one is perhaps a partial copy.
For example,
fill_the_string("love", 12) => "lovelovelove"
fill_the_string("truth", 12) => "truthtruthtr"
-
Write a recursive function sum_up that has no parameters but takes input
from the keyboard. This function prompts the user with
"Enter the amount of numbers to sum: " , followed by "Enter an integer: "
which will read input the amount of times as the number entered before. The function then prints a
message "The sum is n." , where n is the sum of all the integers that were
entered.
For example,
Enter the amount of numbers to sum: 4
Enter an integer: 3
Enter an integer: 56
Enter an integer: 7
Enter an integer: 8
The sum is 74
We've seen the function str.count() in lectures. Using recursion, implement a version
of the function, called my_string_count(s, c) where s is any string, and
c is a string of length one. my_string_count(s, c) will return the number of times that
the character c appears in the string s .
For example,
my_string_count("hello world", "l") => 3
my_string_count("abracadabra", "e") => 0
my_string_count("", "e") => 0
|
Last modified on
Friday, 31 January 2020, at 09:18.