Skip to content

Instantly share code, notes, and snippets.

@Kreastr
Created October 2, 2023 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kreastr/025a1fd08189d736fbe8fe3132a0cb1c to your computer and use it in GitHub Desktop.
Save Kreastr/025a1fd08189d736fbe8fe3132a0cb1c to your computer and use it in GitHub Desktop.
# (c) 2023 Aleksei Romanenko, Pterippi Oy
# Licensed under CC BY-SA 4.0
from random import shuffle
def xor_base_n(a, b, n):
r = ""
for ai,bi in zip(list(a),list(b)):
r += str((int(ai) + int(bi)) % n)
return r
def zeropad(s, l):
while len(s) < l:
s = '0' + s
return s
def rebase(value, base):
result = ""
while value:
value, reminder = divmod(value, base)
result += str(reminder)
return result[::-1]
def find_non_repeating_n_times(arr, n):
print(sum(arr))
pad_len = len(rebase(max(arr),n))
xr = "0" * pad_len
for i in arr:
xr = xor_base_n(zeropad(rebase(i, n), pad_len), xr, n)
print("new value", i, "or", zeropad(rebase(i, n), pad_len), "base", n, " -> accumulated", xr)
return int(xr, n)
arr = [12, 1, 1, 7, 1, 12, 1]
print("Odd one ", find_non_repeating_n_times(arr, 2))
arr = [12, 1, 1, 5, 1, 12, 12]
print("Odd one ", find_non_repeating_n_times(arr, 3))
arr = [1, 2, 5, 10, 22, 37] * 9 + [42]
shuffle(arr)
print("Odd one ", find_non_repeating_n_times(arr, 9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment