def rreduce(message): newMessage = r(lambda x, y: x[:-1]\ if (len(x) > 0 and x[-1].isupper() and y.islower() and x[-1].lower() == y.lower())\ else (x[:-1] if len(x) > 0 and x[-1].islower() and y.isupper() and x[-1].lower() == y.lower()\ else x + y),\ message) if len(newMessage) != len(message): return rreduce(newMessage) else: return message
def find_difference(a, b): return abs(r(sub, [r(mul, a), r(mul, b)]))
def get_average(num_list): total = r(lambda total, element: total + element, num_list) return total / len(num_list)
#!/usr/bin/python3 from functools import reduce as r print(r(lambda x, y: (x + y), map(chr, range(65, 91))))
# My Solution from functools import reduce as r num_list = [1, 2, 3, 4, 5, 6] num_total = r(lambda a, b: a + b, num_list) num_count = len(num_list) num_average = num_total / num_count print(num_average) # JH Solution def get_average(num_list): total = r(lambda total, element: total + element, num_list) return total / len(num_list) print(get_average(num_list))
# Setup from aocd import data from collections import Counter as c from functools import reduce as r from operator import mul as m d = data.split('\n') # Longform # answer = reduce( mul, ( sum( ( repetition in count.values() ) for count in ( Counter(box_id) for box_id in data) ) for repetition in (2,3) ) ) # Golfed s=string, h=hash, x=generic number a=r(m,(sum((x in h.values())for h in(c(s)for s in d))for x in(2,3))) # Output print(a)
import operator as o from functools import reduce as r print(o.add(1, 2)) print(r(lambda x, y: x + y, range(100))) def Sum(): s = 0 for i in range(100): s += i return s print(Sum())
def simulateReduce(L1): return r(lambda x, y: x + y, L1)
from functools import reduce as r from operator import or_, and_ with open("input.txt", "r") as f: xs = [x.split("\n") for x in f.read().split("\n" * 2)] s = lambda fn: sum(len(r(fn, map(set, x))) for x in xs) print("Part 1:", s(or_)) print("Part 2:", s(and_))
def sum(*args): return r(lambda x, y: to_int(x) + to_int(y), args, 0)
def find_difference(a, b): s = lambda x, y: x * y return abs(r(s, a) - r(s, b))
# Reduce function example from functools import reduce as r list_1 = [1, 2, 3, 4] result = r((lambda x, y: x * y), list_1) print(result) # Output : 24