diff --git a/basic/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/basic/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/basic/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/basic/Untitled.ipynb b/basic/Untitled.ipynb new file mode 100644 index 0000000..ba42c26 --- /dev/null +++ b/basic/Untitled.ipynb @@ -0,0 +1,488 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "l = [1,2,2,3,5,5,5,4,2,2,2,2,2,3,4,5,4]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 2, 2, 2, 2, 2, 3, 4, 5, 4]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "l.sort()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 5]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for i in l:\n", + " for nums in range(0,len(l-1)):\n", + " if i" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 5, 4, 2, 2, 3, 4, 5, 4]\n" + ] + } + ], + "source": [ + "for nums in range(1,(len(l)-2)):\n", + " if l[nums-1] == l[nums]:\n", + " l.pop(nums)\n", + " \n", + "print(l)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(l)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 5]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l.pop(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 5]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "s = [1,2,3]" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(s)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m: list index out of range" + ] + } + ], + "source": [ + "s[3]" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "l = range(0,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n" + ] + } + ], + "source": [ + "for num in range(0,len(s)):\n", + " print (num)" + ] + }, + { + "cell_type": "code", + "execution_count": 144, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2]\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "12\n", + "13\n", + "14\n", + "[2, 4, 2, 3, 2, 8, 9]\n" + ] + } + ], + "source": [ + " nums = [2, 2,4,2,2,2,2,2,2, 3, 3, 3, 2,8,9]\n", + " listfinal = []\n", + " listfinal.append(nums[0])\n", + " print (listfinal)\n", + " for num in range(1,len(nums)):\n", + " print (num)\n", + " if nums[num] != nums[num-1]:\n", + " listfinal.append(nums[num])\n", + " print (listfinal)\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4}" + ] + }, + "execution_count": 130, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set(nums)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 124, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nums[11]" + ] + }, + { + "cell_type": "code", + "execution_count": 145, + "metadata": {}, + "outputs": [], + "source": [ + "l1 = [1,2,3,2,4]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "l2 = [2,3,2,1,3]" + ] + }, + { + "cell_type": "code", + "execution_count": 146, + "metadata": {}, + "outputs": [], + "source": [ + "list2= [2,3,4,3,2]" + ] + }, + { + "cell_type": "code", + "execution_count": 148, + "metadata": {}, + "outputs": [], + "source": [ + "l3= l1 + list2" + ] + }, + { + "cell_type": "code", + "execution_count": 149, + "metadata": {}, + "outputs": [], + "source": [ + "l3.sort()" + ] + }, + { + "cell_type": "code", + "execution_count": 150, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 2, 2, 2, 3, 3, 3, 4, 4]" + ] + }, + "execution_count": 150, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..a6a5fac 100755 --- a/basic/list1.py +++ b/basic/list1.py @@ -22,8 +22,11 @@ # Note: python does not have a ++ operator, but += works. def match_ends(words): # +++your code here+++ - return - + count = 0 + for word in words: + if len(word) >= 2 and word[0] == word[len(word)-1]: + count = count + 1 + return count # B. front_x # Given a list of strings, return a list with the strings @@ -34,7 +37,19 @@ def match_ends(words): # before combining them. def front_x(words): # +++your code here+++ - return + listx = [] + listabc = [] + for word in words: + if word[0] == 'x': + listx.append(word) + else: + listabc.append(word) + listx.sort() + listabc.sort() + listfinal = listx + listabc + + + return listfinal # C. sort_last @@ -43,9 +58,22 @@ def front_x(words): # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use a custom key= function to extract the last element form each tuple. -def sort_last(tuples): +def sort_last(tuples): # +++your code here+++ - return + listoflast = [] + listfinal = [] + for tuple in tuples: + last = tuple[len(tuple)-1] + listoflast.append(last) + listoflast.sort() + for nums in listoflast: + for tuple in tuples: + if tuple[len(tuple)-1] == nums: + listfinal.append(tuple) + + + + return listfinal # Simple provided test() function used in main() to print diff --git a/basic/list2.py b/basic/list2.py index 390dd1f..72f761d 100755 --- a/basic/list2.py +++ b/basic/list2.py @@ -14,7 +14,14 @@ # modify the passed in list. def remove_adjacent(nums): # +++your code here+++ - return + listfinal = [] + if nums != []: + listfinal.append(nums[0]) + for num in range(1,len(nums)): + if nums[num] != nums[num-1]: + listfinal.append(nums[num]) + + return listfinal # E. Given two lists sorted in increasing order, create and return a merged @@ -22,9 +29,10 @@ def remove_adjacent(nums): # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, list2): - # +++your code here+++ - return + listfinal = list1 + list2 + listfinal.sort() + return listfinal # Note: the solution above is kind of cute, but unforunately list.pop(0) # is not constant time with the standard python list implementation, so diff --git a/basic/string1.py b/basic/string1.py index 34d2716..0fd6e72 100755 --- a/basic/string1.py +++ b/basic/string1.py @@ -25,7 +25,11 @@ # and donuts(23) returns 'Number of donuts: many' def donuts(count): # +++your code here+++ - return + if count < 10: + a = 'Number of donuts: '+str(count) + else: + a = 'Number of donuts: many' + return a # B. both_ends @@ -35,7 +39,11 @@ def donuts(count): # is less than 2, return instead the empty string. def both_ends(s): # +++your code here+++ - return + if len(s)>1: + a = s[0] + s[1] + s[len(s)-2] + s[len(s)-1] + else: + a = '' + return a # C. fix_start @@ -49,7 +57,10 @@ def both_ends(s): # where all instances of stra have been replaced by strb. def fix_start(s): # +++your code here+++ - return + stra = s[0] + b = s.replace(s[0], '*') + + return s[0]+b[1:len(b)] # D. MixUp @@ -61,7 +72,8 @@ def fix_start(s): # Assume a and b are length 2 or more. def mix_up(a, b): # +++your code here+++ - return + c = b[0:2] + a[2:len(a)] + ' ' + a[0:2] + b[2:len(b)] + return c # Provided simple test() function used in main() to print diff --git a/basic/string2.py b/basic/string2.py index 631091a..90d43e1 100755 --- a/basic/string2.py +++ b/basic/string2.py @@ -17,7 +17,12 @@ # Return the resulting string. def verbing(s): # +++your code here+++ - return + if len(s) >= 3 and s[len(s)-3:len(s)] != 'ing': + s = s + 'ing' + elif len(s) >= 3: + s = s + 'ly' + + return s # E. not_bad @@ -30,7 +35,17 @@ def verbing(s): # This dinner is good! def not_bad(s): # +++your code here+++ - return + a = s + for index in range(0, len(s)): + for x in range(0,len(s)-index): + # print (s[index]) + if s[index:index+3] == 'not' and s[index + x:index+ x +3] == 'bad' and s[-1] != 'd' : + a = s[0:index] + 'good' + s[-1] + elif s[index:index+3] == 'not' and s[index + x:index+ x +3] == 'bad' : + a = s[0:index] + 'good' + + return a + # F. front_back @@ -41,9 +56,42 @@ def not_bad(s): # Given 2 strings, a and b, return a string of the form # a-front + b-front + a-back + b-back def front_back(a, b): - # +++your code here+++ - return - + afront = [''] + bfront = [''] + aback = [''] + bback = [''] + final = [''] + + if len(a) % 2 == 0: + afront = a[0:round((len(a)/2))] + aback = a[round((len(a)/2)):len(a)] + + elif len(a) > 3: + afront = a[0:round((len(a)/2))+1] + aback = a[round((len(a)/2))+ 1:len(a)] + + elif len(a) < 4 and len(a) % 2 != 0: + afront = a[0:round(len(a)/2)] + aback = a[round(len(a)/2):len(a)] + + + + if len(b) % 2 == 0: + bfront = b[0:round((len(b)/2))] + bback = b[round((len(b)/2)):len(b)] + + elif len(b) > 3: + bfront = b[0:round((len(b)/2))+1] + bback = b[round((len(b)/2))+ 1:len(b)] + + elif len(b) < 4 and len(a) % 2 != 0: + bfront = b[0:round(len(b)/2)] + bback = b[round(len(b)/2):len(b)] + + final = afront + bfront + aback + bback + + return final + # Simple provided test() function used in main() to print # what each function returns vs. what it's supposed to return. diff --git a/basic/wordcount.py b/basic/wordcount.py index a6c7c2e..27fccf5 100755 --- a/basic/wordcount.py +++ b/basic/wordcount.py @@ -24,7 +24,7 @@ 2. For the --topcount flag, implement a print_top(filename) which is similar to print_words() but which prints just the top 20 most common words sorted -so the most common word is first, then the next most common, and so on. +£so the most common word is first, then the next most common, and so on. Use str.split() (no arguments) to split on all whitespace. @@ -39,6 +39,24 @@ import sys +def print_words(filename): + + fh = open('filename') + + + wordcount={} + for words in fh: + words = words.split() + for word in words: + word = word.lower() + #print(word) + if word in wordcount: + wordcount[word] += 1 + else: + wordcount[word] =1 + return wordcount + + # +++your code here+++ # Define print_words(filename) and print_top(filename) functions.