def gameLoop(currentRoom=map['cell']): userInput = stdio.readString() if userInput in currentRoom['actions']: currentRoom = map[currentRoom['actions'][userInput]] if userInput == 'door' and player['Holding'] == 'key': stdio.writeln('\n' + door_exam['desc2']) battleLoop() if currentRoom == 'bone_take': bone_take['taken'] = True else: stdio.writeln() stdio.writeln('Sorry, that\'s not a valid input. Try Again.\n') stdio.write(PROMPT) gameLoop(currentRoom) if bone_take['taken'] == True: player['weapon'] = 'bone' stdio.writeln() keyCheck(currentRoom) stdio.writeln(currentRoom['desc']) stdio.writeln() actionList(currentRoom) stdio.writeln() stdio.write(PROMPT) gameLoop(currentRoom)
def _main(): """ Test client [DO NOT EDIT]. """ text, k = sys.argv[1], int(sys.argv[2]) model = MarkovModel(text, k) a = [] while not stdio.isEmpty(): kgram = stdio.readString() char = stdio.readString() a.append((kgram.replace("-", " "), char.replace("-", " "))) for kgram, char in a: if char == ' ': stdio.writef('freq(%s) = %s\n', kgram, model.kgram_freq(kgram)) else: stdio.writef('freq(%s, %s) = %s\n', kgram, char, model.char_freq(kgram, char))
def main(): inStream = instream.InStream(sys.argv[1]) a = inStream.readAllStrings() while not stdio.isEmpty(): key = stdio.readString() if search(key, a) < 0: stdio.writeln(key)
def main(): stack = Stack() while not stdio.isEmpty(): item = stdio.readString() if item != '-': stack.push(item) else: stdio.write(stack.pop() + ' ') stdio.writeln()
def main(): queue = Queue() while not stdio.isEmpty(): item = stdio.readString() if item != '-': queue.enqueue(item) else: stdio.write(queue.dequeue()) stdio.write(' ') stdio.writeln()
def main(): instream = InStream('misspellings.txt') lines = instream.readAllLines() misspellings = SymbolTable() for line in lines: tokens = line.split(' ') misspellings[tokens[0]] = tokens[1] while not stdio.isEmpty(): word = stdio.readString() if word in misspellings: stdio.write(word + '-->' + misspellings[word])
def main(): hashtable = Hashtable() while not stdio.isEmpty(): a = stdio.readString().lower() hashtable.put(re.sub(r'[^\w\s]', '', a)) count = 1 for key in hashtable: stdio.writeln( str(count) + ": " + key.key + " " + str(hashtable.get(key.key))) count += 1 stdio.writef("%d\n", hashtable.size()) stdio.writeln("Get the: " + str(hashtable.get('thhhhhe'))) stdio.writeln(hashtable.contains("thhhh"))
def main(): import stdio q = RandomQueue() while not stdio.isEmpty(): item = stdio.readString() if item != '-': q.enqueue(item) else: stdio.writeln(q.dequeue()) stdio.writeln('Iterate over all remaining items') for item in q: stdio.writeln(item) stdio.writeln('Printing a string rep of the remaining items') stdio.writeln(q) stdio.writeln('Empty the random queue') while not q.isEmpty(): stdio.writeln(q.dequeue())
def main(): import stdio q = Queue() while not stdio.isEmpty(): item = stdio.readString() if item != '-': q.enqueue(item) else: stdio.writeln(q.dequeue()) stdio.writeln('Iterating over the queue') for item in q: stdio.writeln(item) stdio.writeln('Printing the queue as a string') stdio.writeln(q) stdio.writeln('Emptying the queue') while not q.isEmpty(): stdio.writeln(q.dequeue())
else: if opt[i][j] == opt[i + 1][j + 1] + 1: print x[i], " ", y[j], "1" i = i + 1 j = j + 1 if i < opt_m: for k in range(i, opt_m - 1): print x[k], " -", "2" if j < opt_n: for k in range(j, opt_n - 1): print "- ", y[k], "2" # Read x and y. x = stdio.readString() y = stdio.readString() # Create (M + 1) x (N + 1) matrix opt with elements initialized to 0, where # M and N are lengths of x and y respectively. m = len(x) n = len(y) opt = stdarray.create2D(m + 1, n + 1, 0) opt_m = len(opt) opt_n = len(opt[0]) opt_recursive = stdarray.create2D(m + 1, n + 1, 0) opt_m_recursive = len(opt_recursive) opt_n_recursive = len(opt_recursive[0])
#----------------------------------------------------------------------- import stdio import math from arraystack import Stack #----------------------------------------------------------------------- # Read a fully parenthesized numeric expression from standard input, # evaluate it, and write the resulting number to standard output. ops = Stack() values = Stack() while not stdio.isEmpty(): token = stdio.readString() if token == '+': ops.push(token) elif token == '-': ops.push(token) elif token == '*': ops.push(token) elif token == 'sqrt': ops.push(token) elif token == ')': # Pop, evaluate, and push result. op = ops.pop() value = values.pop() if op == '+': value = values.pop() + value elif op == '-': value = values.pop() - value elif op == '*': value = values.pop() * value elif op == 'sqrt': value = math.sqrt(value) values.push(value) elif token != '(': # Token not operator or paren, so push float value.
turn_score_comp = 0 total_play1 = 0 turn_score1 = 0 c = random.randrange(0, 2) if c == 0: print("Computer's turn") total_comp = comp(total_comp) while total_play1 < 100 and total_comp < 100: print( "Would you like to roll or hold? Input 'r' for roll or 'h' for hold.") stuff_typed = stdio.readString() if stuff_typed == "r": b = random.randrange(1, 7) print("Rolled a ", b) turn_score1 += b if b == 1: turn_score1 = 0 print("Pig out!") if total_play1 >= 100 or total_comp >= 100: break if stuff_typed == "h" or b == 1: if stuff_typed == "h": total_play1 += turn_score1
#----------------------------------------------------------------------- # dragon3.py #----------------------------------------------------------------------- import stdio dragon = stdio.readString() nogard = stdio.readString() stdio.write(dragon + 'L' + nogard) stdio.write(' ') stdio.write(dragon + 'R' + nogard) stdio.writeln() #----------------------------------------------------------------------- # python dragon3.py < input.txt | python dragon3.py | python dragon3.py # FLFLFRFLFLFRFRF FLFLFRFRFLFRFRF
# alignment.py: Reads from standard input, the output produced by # edit_distance.py, i.e., input strings x and y, and the opt matrix. The # program then recovers an optimal alignment from opt, and writes to # standard output the edit distance between x and y and the alignment itself. # Import necessary modules. import stdio import stdarray # Read x, y, and opt from standard input. x = stdio.readString() y = stdio.readString() opt = stdarray.readInt2D() # Compute M and N. M = len(x) N = len(y) # Write edit distance between x and y. stdio.write('Edit distance = ') stdio.write(opt[0][0]) stdio.writeln() # Recover and write an optimal alignment. i = 0 j = 0 while i != M and j != N: if opt[i][j] == opt[i + 1][j] + 2: stdio.writef(x[i] + ' ' + '-' + ' ' + '2' + "\n") i += 1
# alignment.py: Reads from standard input, the output produced by # edit_distance.py, i.e., input strings x and y, and the opt matrix. The # program then recovers an optimal alignment from opt, and writes to # standard output the edit distance between x and y and the alignment itself. import stdarray import stdio # Read x, y, and opt from standard input. x, y = stdio.readString(), stdio.readString() opt = stdarray.readInt2D() # Compute M and N. M, N = len(x), len(y) # Write edit distance between x and y. stdio.writeln('Edit distance = ' + str(opt[0][0])) # Recover and write an optimal alignment. i, j = 0, 0 while (i < M and j < N): if opt[i][j] == opt[i + 1][j] + 2: stdio.writeln(x[i] + ' - 2') i += 1 elif opt[i][j] == opt[i][j + 1] + 2: stdio.writeln('- ' + y[j] + ' 2') j += 1 elif x[i] == y[j]: stdio.writeln(x[i] + ' ' + y[j] + ' 0')
print("Hello! Welcome to CPSC 231 Console Hangman!") print("The secret word looks like:") #shows secret word as dashes secret_word = random.choice(new_list) dashes = "-" * len(secret_word) secret_list = list(secret_word) dashes_list = list(dashes) reveal = dashes_list print("".join(reveal)) print("The number of guesses you have left is:", guesses) while guesses > 0 and "-" in dashes_list: print("What is your next guess?") guess_typed = stdio.readString() all_guesses.append(guess_typed) for i in range(len(secret_list)): if secret_list[i] == guess_typed: dashes_list[i] = secret_list[i] reveal = dashes_list #correct guess if guess_typed in dashes_list: print("".join(reveal)) print("Nice Guess!") #wrong guess else: if guess_typed not in bad_guesses: print("Sorry, there is no", guess_typed) bad_guesses += guess_typed
#----------------------------------------------------------------------- # wordcount.py #----------------------------------------------------------------------- import stdio # Read a sequence of strings from standard input, and write the number # of strings to standard output. count = 0 while not stdio.isEmpty(): word = stdio.readString() count += 1 stdio.writeln('number of words = ' + str(count)) #----------------------------------------------------------------------- # python wordcount.py < tale.txt # number of words = 139043
valField = int(sys.argv[3]) # Build a database from inStream. database = inStream.readAllLines() # Extract keys and values from the database and add them to st. st = SymbolTable() for line in database: tokens = line.split(',') key = tokens[keyField] val = tokens[valField] st[key] = val # Read keys, search st, and write values. while not stdio.isEmpty(): query = stdio.readString() if query in st: stdio.writeln(st[query]) else: stdio.writeln('Not found') #----------------------------------------------------------------------- # python lookup.py amino.csv 0 3 # TTA # Leucine # ABC # Not found # TCT # Serine
import stdarray import stdio # Read w and z. w = stdio.readString() z = stdio.readString() # Create (F + 1) x (G + 1) matrix opt with elements initialized to 0, where # F and G are lengths of w and z respectively. opt = [] F = len(w) G = len(z) # create the matrix opt = [[0 for j in range(G + 1)] for i in range(F + 1)] # Initialize bottom row opt[F][j] (0 <= j <= G) to 2 * (G - j). for j in range(G + 1): opt[F][j] = 2 * (G - j) # Initialize right column opt[i][G] (0 <= i <= F) to 2 * (F - i). for i in range(F + 1): opt[i][G] = 2 * (F - i) # Compute the rest of opt. for j in reversed(range(G)): for i in reversed(range(F)): # of checks if there is a penalty or not if w[i] == z[j]: opt[i][j] = min(opt[i + 1][j + 1], opt[i + 1][j] + 2, opt[i][j + 1] + 2) else:
import stdio a = stdio.readString() b = stdio.readString() if len(a) < len(b): temp = a a = b b = temp for i in range(0, abs(len(a) - len(b))): b = '0' + b reb = 0 reb_no = 0 for i in range(len(a) - 1, -1, -1): res = int(a[i]) + int(b[i]) + reb if res >= 10: reb = 1 reb_no += 1 else: reb = 0 print('The number of remember of a + b is: ' + str(reb_no))
def attackLoop(enemy): stdio.writeln('What do you want to do?') stdio.writeln('\nattack / run\n') stdio.write(PROMPT) battleInput = stdio.readString() if battleInput == 'attack': if player['weapon'] == 'bone': atkDmg = random.randrange(weapon['bone base'], weapon['bone max'] + 1) enemy['Health'] -= atkDmg stdio.writef('\nYou strike the beast for %d damage.\n', atkDmg) else: atkDmg = random.randrange(weapon['none base'], weapon['none max'] + 1) enemy['Health'] -= atkDmg stdio.writef('\nYou strike the beast for %d damage.\n', atkDmg) if enemy['Health'] <= 0: stdio.writeln('The beast is dead. You catch your ' + 'breath and step over the body using ' + 'the walls as support. You slowly make ' + 'your way down the corridor until you\'re ' + 'blinded by sunlight. You\'ve escaped. ' + 'Good job!') stdio.writeln('\nPlay Again?\n') stdio.writeln('\nyes / no\n') stdio.write(PROMPT) tryAgain = stdio.readString() if tryAgain == 'yes': gameLoop() else: sys.exit() elif battleInput == 'run': escapeChance = random.randrange(1, 101) if escapeChance >= 80: stdio.writeln('\nYou make a dash down the hall ' + 'and don\'t stop running until you feel ' + 'the sunshine on your face. You\'ve done it.' + ' You\'ve escaped. Good Job!') stdio.writeln('\nPlay Again?\n') stdio.writeln('\nyes / no\n') stdio.write(PROMPT) tryAgain = stdio.readString() if tryAgain == 'yes': main() elif tryAgain == 'no': sys.exit() else: stdio.writeln('\nYou try to escape but the ' + str(enemy['name']) + ' catches you.') else: stdio.writeln('\nSorry, that\'s not a valid command') attackLoop(enemy) hurtDmg = random.randrange(enemy['attack low'], enemy['attack high'] + 1) player['Health'] -= hurtDmg stdio.writef('\nThe beast strikes you for %d damage.\n\n', hurtDmg) if player['Health'] <= 0: stdio.writeln('Alas, the beast has struck you down.' + ' You\'ve failed to escape. Try again?') stdio.writeln('\nyes / no\n') stdio.write(PROMPT) tryAgain = stdio.readString() if tryAgain == 'yes': main() elif tryAgain == 'no': sys.exit() attackLoop(enemy)
import stdio n = stdio.readString() max = 0 for i in range(0, len(n)): if max < int(n[i]): max = int(n[i]) print(max)
import stdio stdio.write("What's your name?") name = stdio.readString() stdio.writeln('Hi,' + name + 'How are you?')
import stdio import stddraw import stdarray male_heights = [] value = stdio.readString() while not stdio.isEmpty(): value = stdio.readString() new_value = value.split(',') stdio.writeln(new_value) male_heights.append(float(new_value[1])) stdio.writeln(male_heights) stdio.writeln(min(male_heights)) stdio.writeln(max(male_heights)) if value[0] == '"male"': male_hights.append(float(value[1])) male_weights.append(float(value[2])) for i in range(58, 80): if i <= float(value[1]) < i + 1: sdtio.writeln() bars = male_heights n = len(male_heights) a = stdarray.create1D(n, n + 1) stddraw.setXscale(54, 80) stddraw.setYscale(0, 100) stddraw.setPenRadius(0.010) for i in range(n): stddraw.filledRectangle(54, a[i], 80, a[i])
"""Word count program""" import sys import stdarray import stdio word_count = 0 while not stdio.isEmpty(): text = stdio.readString() word_count += 1 if text == "'s": word_count += 1 stdio.write(word_count)
import stdio str1 = stdio.readString() str2 = stdio.readString() if len(str1) > len(str2): a = str1 b = str2 else: a = str2 b = str1 count = 0 length = len(b) for i in range(0, len(a)): temp = a[i:(i + length)] if temp == b: count += 1 stdio.writeln(str(count))