lab02

.py

School

Skyline College *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

3

Uploaded by MasterBuffalo3986

"""C88C Lab 2""" """ C88C Spring 2024: Please credit any folks in C88C that you collaborated with, and any online sources you searched for. Remember, it's OK to ask for help, and to search for topics, but you may not search for specific solutions or copy any code directly. List Collaborators: Credit Any Online Sources (google searches, etc): """ # Question 1 def odd_even(x): """Classify a number as odd or even. >>> odd_even(4) 'even' >>> odd_even(3) 'odd' """ return 'even' if x % 2 == 0 else 'odd' def classify(s): """ Classify all the elements of a sequence as odd or even >>> classify([0, 1, 2, 4]) ['even', 'odd', 'even', 'even'] """ return [odd_even(x) for x in s] #Question 2 def find_word(words, n): """ >>> find_word(["cat", "window", "zookeeper"], 5) 'window' >>> find_word(["cat", "dog", "fish"], 3) 'fish' >>> find_word(["cat", "dog", "bro"], 3) '' >>> find_word(["python", "java", "SQL"], 4) 'python' """ for word in words: if len(word) > n: return word return ''
# Question 3 def if_this_not_that(i_list, this): """ >>> original_list = [1, 2, 3, 4, 5] >>> if_this_not_that(original_list, 3) that that that 4 5 """ for item in i_list: if item > this: print(item) else: print("that") # Question 4 def shuffle(cards): """Return a shuffled list that interleaves the two halves of cards. >>> lst = [1, 2, 3, 4, 5, 6, 7, 8] >>> shuffle(lst) [1, 5, 2, 6, 3, 7, 4, 8] >>> shuffle(range(6)) [0, 3, 1, 4, 2, 5] >>> cards = ['AH', '1H', '2H', '3H', 'AD', '1D', '2D', '3D'] >>> shuffle(cards) ['AH', 'AD', '1H', '1D', '2H', '2D', '3H', '3D'] >>> cards # should not be changed ['AH', '1H', '2H', '3H', 'AD', '1D', '2D', '3D'] """ assert len(cards) % 2 == 0, 'len(cards) must be even' mid = len(cards) // 2 first_half = cards[ :mid] second_half = cards[mid: ] shuffled = [] for i in range(mid): shuffled.append(first_half[i]) shuffled.append(second_half[i]) return shuffled # Question 5 def pairs(n): """Returns a new list containing two element lists from values 1 to n >>> pairs(1) [[1, 1]] >>> x = pairs(2) >>> x [[1, 1], [2, 2]] >>> pairs(5) [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]] >>> pairs(-1)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help