├── count_items.py ├── population.py ├── strings_and_input.py └── swap.py /count_items.py: -------------------------------------------------------------------------------- 1 | """ 2 | File: count_items.py 3 | Author: Xin Li 4 | Purpose: This program it will read an a imput from the 5 | keyboard and open the file, and read it's contents. 6 | Sort the lines by the values, in reverse order. 7 | """ 8 | def main(): 9 | lst = [] 10 | dic= {} 11 | print_lst=[] 12 | lst_new=[] 13 | new_dic={} 14 | word = '' 15 | num = 0 16 | filename = input('File to scan:') 17 | filename.strip() 18 | file = open(filename, 'r') 19 | lines = file.readlines() 20 | for line in lines: 21 | if line[0] != '#': 22 | lst.append(line) 23 | for k in lst: 24 | if k != len(k)*' ': 25 | lst_new.append(k) 26 | for index in lst_new: 27 | index = index.strip('\n') 28 | infor = index.split(' ') 29 | test = False 30 | for i in infor: 31 | if len(i) >= 1: 32 | if i[0].isdigit()== True: 33 | test = True 34 | if i[0] =='-': 35 | test = True 36 | if test == False: 37 | infor = index.split('\t') 38 | if infor[len(infor)-1] != '': 39 | if infor[0] != '': 40 | if infor[0] not in dic: 41 | dic[infor[0]] = infor[len(infor)-1] 42 | print_lst.append(infor[0]) 43 | else: 44 | num=int(dic[infor[0]])+int(infor[len(infor)-1]) 45 | dic[infor[0]] = num 46 | for j in print_lst: 47 | if int(dic[j]) in new_dic: 48 | new_dic[int(dic[j])].append(j) 49 | test_new = True 50 | while test_new == True : 51 | test_new = False 52 | for b in range(len(new_dic[int(dic[j])])-2,-1,-1): 53 | if new_dic[int(dic[j])][b+1] > new_dic[int(dic[j])][b]: 54 | word = new_dic[int(dic[j])][b] 55 | new_dic[int(dic[j])][b] = new_dic[int(dic[j])][b+1] 56 | new_dic[int(dic[j])][b+1] = word 57 | test = True 58 | else: 59 | new_dic[int(dic[j])] = [j] 60 | keys = sorted(new_dic.keys()) 61 | for n in range (len(keys)-1,-1,-1): 62 | for v in range (len(new_dic[keys[n]])-1,-1,-1): 63 | print(new_dic[keys[n]][v], keys[n]) 64 | main() 65 | -------------------------------------------------------------------------------- /population.py: -------------------------------------------------------------------------------- 1 | """ 2 | File: population.py 3 | Author: Xin Li 4 | Purpose: This program will read an input string from the 5 | keyboard and print a prompt and read an input string. 6 | Remove all leading and trailing whitespace from the 7 | input string, and then open a 铿乴e with that name. The 8 | program will then read through that 铿乴e, reading the 9 | lines one at a time, and performing a calculation on 10 | the data. 11 | """ 12 | import os 13 | def main(): 14 | # chdir to the same directory as where this script is ... so 15 | # that open() will open the file we expect. 16 | this_script = os.path.realpath(__file__) 17 | dir_of_script = os.path.dirname(this_script) 18 | os.chdir(dir_of_script) 19 | 20 | string = input('file: ') 21 | string.strip() 22 | file = open(string, 'r') 23 | lines = file.readlines() 24 | lst = [] 25 | lst_new=[] 26 | population = 0 27 | name ='' 28 | total_population = 0 29 | total_num = 0 30 | for line in lines: 31 | if line[0] != '#': 32 | lst.append(line) 33 | for k in lst: 34 | if k[:len(k)-2] != (len(k)-2)*' ': 35 | lst_new.append(k) 36 | for index in lst_new: 37 | index = index.strip('\n') 38 | information = index.split(' ') 39 | test = False 40 | for i in information: 41 | if len(i) >= 1: 42 | if i[0].isdigit()== True: 43 | test = True 44 | if test == False: 45 | information = index.split('\t') 46 | for i in information : 47 | if len(i) >= 1: 48 | if i[0].isalpha() == True: 49 | name += i 50 | if i[0].isdigit() == True: 51 | population += int(i) 52 | total_population += int(i) 53 | print('State/Territory: ',name) 54 | print('Population: ',population) 55 | total_num +=1 56 | print('') 57 | name ='' 58 | population =0 59 | print('# of States/Territories:',total_num) 60 | print('Total Population: ',total_population) 61 | main() 62 | -------------------------------------------------------------------------------- /strings_and_input.py: -------------------------------------------------------------------------------- 1 | """ 2 | File: string_and_input.py 3 | Author: Xin Li 4 | Purpose: This program will read an input string from the 5 | keyboard and print a prompt and save the value which 6 | the user types. 7 | """ 8 | def main(): 9 | string = input('input string: ') 10 | num_1=input() 11 | num_2=input() 12 | num_1 = int(num_1) 13 | num_2 = int(num_2) 14 | length = len(string) 15 | print(length) 16 | second_character = string[1] 17 | print(second_character) 18 | if len(string) >= 10: 19 | first_ten = string[:10] 20 | else: 21 | first_ten = string 22 | print(first_ten) 23 | if len(string) >= 5: 24 | last_five = string[len(string)-5:] 25 | print(last_five) 26 | upper_string = string.upper() 27 | print(upper_string) 28 | string_lower = string.lower() 29 | lst_1 = ['q','w','e','r','t','y'] 30 | if string_lower[0] in lst_1: 31 | print('QWERTY') 32 | lst_2 = ['u','i','o','p'] 33 | if string[0] in lst_2: 34 | print('UIOP') 35 | if string_lower[0] not in lst_1: 36 | if string[0] not in lst_2: 37 | if string[0].isalpha() == True: 38 | print('LETTER') 39 | if string[0].isdigit() == True: 40 | print('DIGIT') 41 | if string[0].isalpha() == False and string[0].isdigit() == False: 42 | print('OTHER') 43 | print(num_1*num_2) 44 | main() 45 | -------------------------------------------------------------------------------- /swap.py: -------------------------------------------------------------------------------- 1 | """ 2 | File: swap.py 3 | Author: Xin Li 4 | Purpose: This program will read an input string from the 5 | keyboard and print a prompt and read an input string. 6 | """ 7 | def main(): 8 | string = input('Please give a string to swap:') 9 | string.strip() 10 | string_new = string[int(len(string) / 2):] + string[:int(len(string) / 2)] 11 | print(string_new) 12 | main() 13 | --------------------------------------------------------------------------------