Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 116 Tutorial 6 Strings, Raw Input, Lists. 1. Write a function convert_format that consumes nothing, but takes keyboard input. The program prompts "Enter.

Similar presentations


Presentation on theme: "CS 116 Tutorial 6 Strings, Raw Input, Lists. 1. Write a function convert_format that consumes nothing, but takes keyboard input. The program prompts "Enter."— Presentation transcript:

1 CS 116 Tutorial 6 Strings, Raw Input, Lists

2 1. Write a function convert_format that consumes nothing, but takes keyboard input. The program prompts "Enter date: ". The inputted string is of the form "dd/mm/yyyy" corresponding to a valid date, where dd is a 1-2 digit number (between 1 and 31, depending on the month), mm is a 1-2 digit number (between 1 and 12), and yyyy is a 1-4 digit integer. The function produces a string of the form "Month dd, yyyy". For example, convert_format() Enter date: 12/10/2012 => "October 12, 2012" convert_format() Enter date: 11/1/1999 => "January 11, 1999" Use string methods and string formatting (using %) to complete this question.

3 A card is a list of length 2 where -the first item is an integer between 1 and 10, inclusive, representing the value of the card, and -the second item is a string ("hearts", "spades", "clubs", or "diamonds") representing the suit of the card.

4 2. Write a function create_cards that consumes a list of card values (integers between 1 and 10) and a list of suit values (one of the four suit strings), and produces a list of cards created pair- wise from the consumed lists. For example, create_cards([4,1,10],["hearts","diamonds","clubs"]) => [[4,"hearts"], [1, "diamonds"], [10, "clubs”]]

5 3. Write a function choose_by_colour that consumes a list of cards ( hand ) and a string "red" or "black" ( colour ) and produces a list of the values of the cards in hand of the appropriate colour (spades and clubs are "black", hearts and diamonds are "red"). For example, choose_by_colour([[1,'hearts'], [9, 'spades'], [3,'diamonds']], 'red') => [1,3]

6 4. Write a function modify_list that consumes a list of integers (called numbers ) and a single integer ( n ). The function produces None, but mutates the list in the following way: - If n does not appear in numbers then add it to the end of numbers. - If n appears once, remove n from numbers. - If n appears at least twice, remove the first and last occurences of n.

7 5. Write a function flip_colour that reads in a card and mutates the suit of that card to a different colour: if the card read in is a heart, it is mutated to a spade (and vice versa), while if the card read in is a club, it is mutated to a diamond (and vice versa). Then write a function flip_hand that reads in a list of cards (a hand), and mutates the suit of each card in the list so that its colour is flipped in this way.

8 6. Write a function sanitize that consumes a string and produces a similar string but with any non- alphanumeric characters removed. For example, sanitize("@Test@") => "Test"


Download ppt "CS 116 Tutorial 6 Strings, Raw Input, Lists. 1. Write a function convert_format that consumes nothing, but takes keyboard input. The program prompts "Enter."

Similar presentations


Ads by Google