Skip to content

Commit

Permalink
Updated Python code to make simplifications relying on Python 3 featu…
Browse files Browse the repository at this point in the history
…res, on 10 pages: "Binary array set", "BitTorrent bencode format tools", "B-tree set", "Forcing a file's CRC to any value", "Free small FFT in multiple languages", "Gauss-Jordan elimination over any field", "Karatsuba multiplication", "Montgomery reduction algorithm", "Optimizing brainfuck compiler", "Windows timestamp accessor library".
  • Loading branch information
nayuki committed Apr 7, 2020
1 parent 7b4d7ce commit 3a62f10
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 21 deletions.
3 changes: 1 addition & 2 deletions binary-array-set/binaryarrayset.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def clear(self):
def __iter__(self):
for vals in self.values:
if vals is not None:
for val in vals:
yield val
yield from vals


# Runs in O((log n)^2) time
Expand Down
1 change: 0 additions & 1 deletion bittorrent-bencode-format-tools/bencode.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
Bencode supports four types of values:
* Integer, which is mapped to Python int.
* Byte string, which is mapped to Python bytes or bytearray.
(Note that in Python 2, bytes is a synonym for str.)
* List, which is mapped to Python list or tuple, such that every element is a bencode value.
* Dictionary, which is mapped to Python dict, such that every key
is a bytes object and every value is a bencode value."""
Expand Down
1 change: 0 additions & 1 deletion bittorrent-bencode-format-tools/decode-bencode-demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# Software.
#

from __future__ import print_function
import os, sys
import bencode

Expand Down
3 changes: 1 addition & 2 deletions btree-set/btreeset.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ def push_left_path(node):
node, index = stack.pop()
if node.is_leaf():
assert index == 0
for obj in node.keys:
yield obj
yield from node.keys
else:
yield node.keys[index]
index += 1
Expand Down
4 changes: 2 additions & 2 deletions forcing-a-files-crc-to-any-value/forcecrc32.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def main(args):

# ---- Main function ----

# Public library function. path is str/unicode, offset is uint, newcrc is uint32, printstatus is bool.
# Public library function. path is str, offset is uint, newcrc is uint32, printstatus is bool.
# Returns None. May raise IOError, ValueError, AssertionError.
def modify_file_crc32(path, offset, newcrc, printstatus=False):
with open(path, "r+b") as raf:
Expand Down Expand Up @@ -107,7 +107,7 @@ def get_crc32(raf):
while True:
buffer = raf.read(128 * 1024)
if len(buffer) == 0:
return reverse32(crc & MASK)
return reverse32(crc)
crc = zlib.crc32(buffer, crc)


Expand Down
2 changes: 1 addition & 1 deletion free-small-fft-in-multiple-languages/fft-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _naive_convolution(x, y):

# ---- Utility functions ----

_maxlogerr = float("-inf")
_maxlogerr = -math.inf

def _log10_rms_err(x, y):
global _maxlogerr
Expand Down
4 changes: 2 additions & 2 deletions gauss-jordan-elimination-over-any-field/fieldmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# https://www.nayuki.io/page/gauss-jordan-elimination-over-any-field
#

import fractions
import fractions, math


# ---- Field abstract class ----
Expand Down Expand Up @@ -366,7 +366,7 @@ def __init__(self, a, b, c, d):
a = -a
b = -b
c = -c
gcd = fractions.gcd(fractions.gcd(a, b), c)
gcd = math.gcd(math.gcd(a, b), c)
if gcd != 1:
a //= gcd
b //= gcd
Expand Down
4 changes: 1 addition & 3 deletions karatsuba-multiplication/karatsuba.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#
# Karatsuba fast multiplication algorithm (Python)
#
# Copyright (c) 2014 Project Nayuki
# Copyright (c) 2020 Project Nayuki
# All rights reserved. Contact Nayuki for licensing.
# https://www.nayuki.io/page/karatsuba-multiplication
#

# Requires Python version >= 2.7 because of long.bit_length().


# Requirement: _CUTOFF >= 64, or else there will be infinite recursion.
_CUTOFF = 1536
Expand Down
4 changes: 2 additions & 2 deletions montgomery-reduction-algorithm/montgomery-reducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# https://www.nayuki.io/page/montgomery-reduction-algorithm
#

import fractions, random, unittest
import math, random, unittest


class MontgomeryReducerTest(unittest.TestCase):
Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self, mod):
self.reducerbits = (mod.bit_length() // 8 + 1) * 8 # This is a multiple of 8
self.reducer = 1 << self.reducerbits # This is a power of 256
self.mask = self.reducer - 1
assert self.reducer > mod and fractions.gcd(self.reducer, mod) == 1
assert self.reducer > mod and math.gcd(self.reducer, mod) == 1

# Other computed numbers
self.reciprocal = MontgomeryReducer.reciprocal_mod(self.reducer % mod, mod)
Expand Down
4 changes: 2 additions & 2 deletions optimizing-brainfuck-compiler/bfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def main(args):

inname = args[0]
if not os.path.isfile(inname):
return inname + ": Not a file"
return f"{inname}: Not a file"

outname = args[1]
if outname.endswith(".c" ): outfunc = commands_to_c
elif outname.endswith(".java"): outfunc = commands_to_java
elif outname.endswith(".py" ): outfunc = commands_to_python
else: return outname + ": Unknown output type"
else: return f"{outname}: Unknown output type"

# Read input
with open(inname, "rt") as fin:
Expand Down
1 change: 0 additions & 1 deletion windows-timestamp-accessor-library/wintimestamp-demo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: UTF-8 -*-
#
# Windows timestamp accessor demo (Python)
#
Expand Down
4 changes: 2 additions & 2 deletions windows-timestamp-accessor-library/wintimestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __exit__(self, type, value, traceback):
self.close()


# Get-methods return the number of ticks as an int; path can be of type str or unicode
# Get-methods return the number of ticks as an int; path is of type str

def get_creation_time(self, path):
return self._get_some_time("Creation", path)
Expand All @@ -59,7 +59,7 @@ def _get_some_time(self, type, path):
return int(tokens[1])


# Set-methods require the number of ticks to be an int; path can be of type str or unicode
# Set-methods require the number of ticks to be an int; path is of type str

def set_creation_time(self, path, ticks):
self._set_some_time("Creation", path, ticks)
Expand Down

0 comments on commit 3a62f10

Please sign in to comment.