def decode(digits, base): """Decode given digits in given base to number in base 10. digits: str -- string representation of number (in given base) base: int -- base of given number return: int -- integer representation of number (in base 10)""" # Handle up to base 36 [0-9a-z] assert 2 <= base <= 36, 'base is out of range: {}'.format(base) # TODO: Decode digits from binary (base 2) # ... # TODO: Decode digits from hexadecimal (base 16) # ... # TODO: Decode digits from any base (2 up to 36) # ... digits = digits[::-1] decode_num = 0 for i in range(len(digits)): if digits[i].isalpha(): digit = string.ascii_lowercase(digits[i].lower()) + 10 else: # power = len(digits) - i - 1 # Get the power #digit * base ^ power digit = int(digits[i]) decode_num += digit * base**i #Another way to do this would be #decode_num += digit * (base ** (len(digits) - i - 1)) return decode_num
def __str__(self): if self.type == EDGE: return self.x elif self.x < 26: return string.ascii_lowercase(self.x) else: return '%s(%s)' % (('Edge', 'Node')[self.type], self.x)
def encode(s): d = {" ": ""} a = list(string.ascii_lowercase()) reverse = a[::-1] for i in range(len(a)): d[a[i]] = reverse[i] for x in list(s): if x in d: list(s).replace(x, d[x], 1) r = " ".join(list(s), 5) return r
def rot13(s): rot13_upper = string.ascii_uppercase[13:] + string.ascii_uppercase[13:] rot13_lower = string.ascii_lowercase[13:] + string.ascii_lowercase[13:] result = "" for c in s: if c in string.ascii_uppercase: result += rot13_upper[string.ascii_uppercase.find(c)] elif c in string.ascii_lowercase: result += rot13_lower[string.ascii_lowercase(c)] else: result += c
def getAvailableLetters(lettersGuessed): ''' lettersGuessed: list, what letters have been guessed so far returns: string, comprised of letters that represents what letters have not yet been guessed. ''' # if it's aseperated function, need to add“import string” avaliableLetter = '' for i in string.ascii_lowercase(): if i not in lettersGuessed: avaliableLetter += i return avaliableLetter
def randPass(): randSrc = string.ascii_letters + string.digits + string.punctuation password = random.choice(string.ascii_lowercase(num_lower)) password += random.choice(string.ascii_uppercase(num_upper)) password += random.choice(string.digits(num_num)) password += random.choice(string.punctuation) for i in len(length): password += random.choice(randSrc) passwordList = list(password) random.SystemRandom().shuffle(passwordList) password = '******'.join(passwordList) return password
def main(): print("This program creates a file of usernames from a file of names.") infilename = input("What file are the names in? ") outfilename = input("What file should the usernames go in? ") infile = open(infilename, 'r') outfile = open(outfilename, 'w') for line in infile: first, last = line.split() uname = string.ascii_lowercase(first[0] + last[:7]) outfile.write(uname + '\n') infile.close() outfile.close() print("Usernames have been written to", outfilename)
def get_remaining_letters(letters_guessed): """ Determine the letters that have not been guessed Args: letters_guessed: list (of strings), which letters have been guessed Returns: String, comprised of letters that haven't been guessed yet. """ # TODO: Fill in your code here available = '' from string import ascii_lowercase for ch in ascii_lowercase: #For each character in ascii lowercase, if the character is in letters_guessed, the available letters are ascii_lowercase but without ones in letters_guessed if ch in letters_guessed: available = (ascii_lowercase(letters_guessed)) print('Available letters:', available)
import random from flask import Flask from flask_restful import Api, Resource, reqparse, abort from collections import defaultdict import unittest2 import flaskapi import string app = Flask(__name__) api = Api(app) parser = reqparse.RequestParser() parser.add_argument('word') parser.add_argument('wordlist', action='append') #build useful dicts/lists alphabet = list(string.ascii_lowercase()) #import CMU dictionary idea for method from: # https://github.com/aparrish/gen-text-workshop/blob/master/cmu_pronouncing_dictionary_notes.md rhymedict = defaultdict(list) worddict = {} with open('cmudict-0.7b.txt') as f: for line in f: #skip comments if(line[:3]==';;;'): continue #split elements on the double space word, sounds = line[:-1].split(' ') sounds = sounds.split(' ') if(len(sounds)==1): rhyme = sounds[0]
def yes(ques): from string import ascii_lowercase ans = ascii_lowercase(input(ques)) return ans[0:1] == 'y'
def ispangram(str1, alpha=string.ascii_lowercase()): return true
# Exercise 2 word = 'New England Patriots' count = 0 for letter in word: if letter == 'a': count = count + 1 print(count) team = 'New England Patriots' index = team.find('a') print(index) # Exercise 4 import string alphabet = string.ascii_lowercase() def price_find(food): for i in alphabet: price[i] = (ord(i)) - 96 sum(price) food = 'bananas' print(price_find(food)) # Exercise 5 def any_lowercase1(s): for c in s: if c.islower(): return True else: return False