# 0 |-|-|-|-| # 1 |-|-|-|-| # 2 |-|-|-|-| # 3 |-|-|-|-| output = "" # Column index header: row_size = ll.length(new_board) output += " " for col_index in range(0, row_size): output += str(col_index) + " " output += "\n" # Rows to string: for row in new_board: (row_index, _, d) = ll.head(row) rest_rows = ll.tail(row) row_str = "|" + d for (r, c, d) in rest_rows: cell = "|" + d row_str += cell # output = output + cell if row_index < 10: row_str = " " + str(row_index) + " " + row_str + "|\n" else: row_str = str(row_index) + " " + row_str + "|\n" output += row_str print(output) # Redo do this problem using a list comprehension.
import list_lib as ll # Treating strings as lists and using for loops. Factor out of the list # words every string that begins with the letter 'a' or the letter 't'. words = [ "The", "happy", "student", "did", "their", "homework", "and", "ate", "an", "apple" ] result = [] skips = ['T', 't', 'a', 'A'] for word in words: first_char = ll.head(word) if not (first_char in skips): result = ll.cons(word, result) result = ll.reverse(result) # Define resolve the problem using a list comprehension, and store your result in # result2. result2 = [word for word in words if not (ll.head(word) in skips)]
## nums_sum += n ## if nums_sum > 50: ## break ## nums_len -= 1 ##print("The sum is "+str(nums_sum)) for x in nums: print(str(x)) print(nums) print("-----------------------------") x = 0 proc_nums = nums while nums_len > 0: x = ll.head(proc_nums) proc_nums = ll.tail(proc_nums) print(str(x)) nums_len -= 1 print(nums) ##power_off = false ##while True: ## ... ## if power_off: ## break ##while !stopButtonUp: ## ... # Continue : Ends the iteration early (forces loop to return to the condition).
import list_lib as ll # Write the following list comprehension using an explicit for-loop: # # squares = [x**2 for x in range(0,43)] # # Then write a for-loop to print the list out to the screen in list format. squares = [] for x in range(0,43): squares = ll.cons(x**2,squares) squares = ll.reverse(squares) output = "[" for x in range(0,ll.length(squares) - 1): next_element = str(ll.head(squares)) squares = ll.tail(squares) output = output + next_element + ", " last_element = str(ll.head(squares)) output = output + last_element + "]" print(output)
import list_lib as ll # Treating strings as lists and using for loops. Factor out of the list # words every string that begins with the letter 'a' or 'A' or the letter 't' or 'T'. words = [ "The", "happy", "student", "did", "their", "homework", "and", "ate", "an", "apple" ] skips = ['a', 'A', 't', 'T'] found_words = [] for word in words: first_char = ll.head(word) if not (first_char in skips): found_words = ll.cons(word, found_words) found_words = ll.reverse(found_words) # Define resolve the problem using a list comprehension, and store your result in # found_words2. found_words2 = [word for word in words if not (ll.head(word) in skips)]
("Stephanie", "Doe", 50505050), ("Andy", "Bernard", 606060606)] new_students = [] length = ll.length(students) ##while length > 0: ## (first, last, sid) = ll.head(students) ## students = ll.tail(students) ## length -= 1 ## ## if first == "Stephanie" and last == "Doe": ## continue ## else: ## sid += 1 ## new_students = ll.cons((first, last, sid),new_students) first_name = "Pam" last_name = "Beesly" found_sid = 0 while length > 0: (first, last, sid) = ll.head(students) students = ll.tail(students) length -= 1 if first == first_name and last == last_name: found_sid = sid break # stop looking now! print("Still looking..") print("Found: " + str(found_sid))
import list_lib as ll # Write the following list comprehension using an explicit for-loop: # # squares = [x**2 for x in range(0,43)] # # Then write a for-loop to print the list out to the screen in list format. squares = [] for x in range(0, 43): squares = ll.cons(x**2, squares) squares = ll.reverse(squares) # 1: x = 0, squares = cons(x**2,squares) = cons(0**2,[]) = cons(0,[]) = [0] # 2: x = 1, squares = cons(x**2,[0]) = cons(1**2,[0]) = cons(1,[0]) = [1,0] # 3: x = 2, squares = cons(x**2,[1,0]) = cons(2**2,[1,0]) = cons(4,[1,0]) = [4,1,0] # When trying to output structured data, create the desired output using variables, # and then output the variable. output = "[" for i in range(0, ll.length(squares) - 1): sq = ll.head(squares) squares = ll.tail(squares) output = output + str(sq) + ", " sq = ll.head(squares) output = output + str(sq) + "]" print(output)