Exemple #1
0
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)]
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)
Exemple #3
0
import list_lib as ll

# Using for-loops and given a blank 10 x 10 board called board.
# Mark every other cell with a X.  Then print the board out in
# for format of a game board.

board = [[(r, c, '-') for c in range(0, 10)] for r in range(0, 10)]
new_board = []
for row in board:
    new_row = []
    for (r, c, d) in row:
        if c % 2 == 0:
            new_row = ll.cons((r, c, "X"), new_row)
        else:
            new_row = ll.cons((r, c, d), new_row)
    new_row = ll.reverse(new_row)
    new_board = ll.cons(new_row, new_board)
new_board = ll.reverse(new_board)

output = "  "
row_i = 0
for col_i in range(0, 10):
    output = output + " " + str(col_i)
output = output + "\n"

for row in new_board:
    if row_i < 10:
        output = output + " " + str(row_i) + "|"
    else:
        output = output + str(row_i) + "|"
    for (r, c, d) in row:
Exemple #4
0
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)]