Skip to content

Modernize Python 2 code to get ready for Python 3 AGAIN #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions File_Transfer_Protocol/ftp_client_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
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')
Expand All @@ -42,7 +42,7 @@
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)
Expand All @@ -55,4 +55,4 @@
f.close()
print('Successfully get the file')
s.close()
print('connection closed')
print('connection closed')
17 changes: 8 additions & 9 deletions Project Euler/Problem 29/solution.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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__':
Expand Down
8 changes: 4 additions & 4 deletions data_structures/Trie/Trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self):
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
Expand All @@ -21,7 +21,7 @@ def insert_many(self, words: [str]): # noqa: F821 This syntax is Python 3 only
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
Expand All @@ -34,7 +34,7 @@ def insert(self, word: str): # noqa: F821 This syntax is Python 3 only
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
Expand All @@ -48,7 +48,7 @@ def find(self, word: str) -> bool: # noqa: F821 This syntax is Python 3 only
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
Expand Down
4 changes: 3 additions & 1 deletion dynamic_programming/abbreviation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
a=daBcd and b="ABC"
daBcd -> capitalize a and c(dABCd) -> remove d (ABC)
"""


def abbr(a, b):
n = len(a)
m = len(b)
Expand All @@ -26,4 +28,4 @@ def abbr(a, b):


if __name__ == "__main__":
print abbr("daBcd", "ABC") # expect True
print(abbr("daBcd", "ABC")) # expect True
4 changes: 2 additions & 2 deletions dynamic_programming/fastfibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@


# 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)
Expand Down
6 changes: 3 additions & 3 deletions machine_learning/scoring_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return score