From 4ee0e620cbd15bf73143b3c5ffa63304551ddb31 Mon Sep 17 00:00:00 2001 From: cclauss Date: Sun, 21 Jan 2018 08:25:19 +0100 Subject: [PATCH] Modernize Python 2 code to get ready for Python 3 AGAIN --- File_Transfer_Protocol/ftp_client_server.py | 22 ++++++++++----------- Project Euler/Problem 29/solution.py | 17 ++++++++-------- data_structures/Trie/Trie.py | 8 ++++---- dynamic_programming/abbreviation.py | 4 +++- dynamic_programming/fastfibonacci.py | 4 ++-- machine_learning/scoring_functions.py | 6 +++--- 6 files changed, 31 insertions(+), 30 deletions(-) diff --git a/File_Transfer_Protocol/ftp_client_server.py b/File_Transfer_Protocol/ftp_client_server.py index ff7a8ec2e..f73f85843 100644 --- a/File_Transfer_Protocol/ftp_client_server.py +++ b/File_Transfer_Protocol/ftp_client_server.py @@ -8,21 +8,21 @@ host = socket.gethostname() # Get local machine name s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. -print 'Server listening....' +print('Server listening....') while True: conn, addr = s.accept() # Establish connection with client. - print 'Got connection from', addr + print('Got connection from', addr) data = conn.recv(1024) print('Server received', repr(data)) - filename='mytext.txt' - f = open(filename,'rb') - l = f.read(1024) - while (l): - conn.send(l) - print('Sent ',repr(l)) - l = f.read(1024) + filename = 'mytext.txt' + f = open(filename, 'rb') + in_data = f.read(1024) + while (in_data): + conn.send(in_data) + print('Sent ', repr(in_data)) + in_data = f.read(1024) f.close() print('Done sending') @@ -42,7 +42,7 @@ s.connect((host, port)) s.send("Hello server!") with open('received_file', 'wb') as f: - print 'file opened' + print('file opened') while True: print('receiving data...') data = s.recv(1024) @@ -55,4 +55,4 @@ with open('received_file', 'wb') as f: f.close() print('Successfully get the file') s.close() -print('connection closed') \ No newline at end of file +print('connection closed') diff --git a/Project Euler/Problem 29/solution.py b/Project Euler/Problem 29/solution.py index 9d6148da3..64d35c84d 100644 --- a/Project Euler/Problem 29/solution.py +++ b/Project Euler/Problem 29/solution.py @@ -1,16 +1,18 @@ def main(): - """ + """ Consider all integer combinations of ab for 2 <= a <= 5 and 2 <= b <= 5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, 35=243 42=16, 43=64, 44=256, 45=1024 52=25, 53=125, 54=625, 55=3125 - If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: + If they are then placed in numerical order, with any repeats removed, + we get the following sequence of 15 distinct terms: 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 - How many distinct terms are in the sequence generated by ab for 2 <= a <= 100 and 2 <= b <= 100? + How many distinct terms are in the sequence generated by ab + for 2 <= a <= 100 and 2 <= b <= 100? """ collectPowers = set() @@ -19,15 +21,12 @@ def main(): N = 101 # maximum limit - for a in range(2,N): - - for b in range (2,N): - + for a in range(2, N): + for b in range(2, N): currentPow = a**b # calculates the current power collectPowers.add(currentPow) # adds the result to the set - - print "Number of terms ", len(collectPowers) + print("Number of terms ", len(collectPowers)) if __name__ == '__main__': diff --git a/data_structures/Trie/Trie.py b/data_structures/Trie/Trie.py index 63e291b97..b6234c670 100644 --- a/data_structures/Trie/Trie.py +++ b/data_structures/Trie/Trie.py @@ -12,7 +12,7 @@ class TrieNode: self.nodes = dict() # Mapping from char to TrieNode self.is_leaf = False - def insert_many(self, words: [str]): # noqa: F821 This syntax is Python 3 only + def insert_many(self, words: [str]): # noqa: E999 This syntax is Python 3 only """ Inserts a list of words into the Trie :param words: list of string words @@ -21,7 +21,7 @@ class TrieNode: for word in words: self.insert(word) - def insert(self, word: str): # noqa: F821 This syntax is Python 3 only + def insert(self, word: str): # noqa: E999 This syntax is Python 3 only """ Inserts a word into the Trie :param word: word to be inserted @@ -34,7 +34,7 @@ class TrieNode: curr = curr.nodes[char] curr.is_leaf = True - def find(self, word: str) -> bool: # noqa: F821 This syntax is Python 3 only + def find(self, word: str) -> bool: # noqa: E999 This syntax is Python 3 only """ Tries to find word in a Trie :param word: word to look for @@ -48,7 +48,7 @@ class TrieNode: return curr.is_leaf -def print_words(node: TrieNode, word: str): # noqa: F821 This syntax is Python 3 only +def print_words(node: TrieNode, word: str): # noqa: E999 This syntax is Python 3 only """ Prints all the words in a Trie :param node: root node of Trie diff --git a/dynamic_programming/abbreviation.py b/dynamic_programming/abbreviation.py index 44a168980..f4d07e402 100644 --- a/dynamic_programming/abbreviation.py +++ b/dynamic_programming/abbreviation.py @@ -10,6 +10,8 @@ Example: a=daBcd and b="ABC" daBcd -> capitalize a and c(dABCd) -> remove d (ABC) """ + + def abbr(a, b): n = len(a) m = len(b) @@ -26,4 +28,4 @@ def abbr(a, b): if __name__ == "__main__": - print abbr("daBcd", "ABC") # expect True + print(abbr("daBcd", "ABC")) # expect True diff --git a/dynamic_programming/fastfibonacci.py b/dynamic_programming/fastfibonacci.py index 2c3960d11..66d2b2ff0 100644 --- a/dynamic_programming/fastfibonacci.py +++ b/dynamic_programming/fastfibonacci.py @@ -7,14 +7,14 @@ import sys # returns F(n) -def fibonacci(n: int): # noqa: F821 This syntax is Python 3 only +def fibonacci(n: int): # noqa: E999 This syntax is Python 3 only if n < 0: raise ValueError("Negative arguments are not supported") return _fib(n)[0] # returns (F(n), F(n-1)) -def _fib(n: int): # noqa: F821 This syntax is Python 3 only +def _fib(n: int): # noqa: E999 This syntax is Python 3 only if n == 0: # (F(0), F(1)) return (0, 1) diff --git a/machine_learning/scoring_functions.py b/machine_learning/scoring_functions.py index 861d45c1f..a2d97b09d 100755 --- a/machine_learning/scoring_functions.py +++ b/machine_learning/scoring_functions.py @@ -70,9 +70,9 @@ def mbd(predict, actual): difference = predict - actual numerator = np.sum(difference) / len(predict) denumerator = np.sum(actual) / len(predict) - print str(numerator) - print str(denumerator) + print(numerator) + print(denumerator) score = float(numerator) / denumerator * 100 - return score \ No newline at end of file + return score