Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): print(t, k, ls) list_sorted = sorted(ls) x = sum(list_sorted[:k]) if x > t: return None else: new_list = [] for i in range(0, len(ls) + 1): for subset in itertools.combinations(ls, k): if sum(subset) <= t: new_list.append(sum(subset)) if len(new_list) == 0: return None else: return max(new_list)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): li=[] for x in itertools.combinations(ls, k): li.append(sum(x)) m=-1 for s in li: if s <= t and s > m: m=s return None if m==-1 else m
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): sorted_ls = sorted(ls) for distance in sorted_ls: if distance > t: sorted_ls.remove(distance) if len(sorted_ls) < k: return None comb = combinations(sorted_ls, k) sums = [] for combination in comb: if sum(combination) <= t: sums.append(sum(combination)) if sums == []: return None return max(sums)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): if k > len(ls): return None combos = list(combinations(ls, k)) sums = [sum(x) for x in combos if sum(x) <= t] if sums: best = min(sums, key=lambda x: t-x) # find smallest diff return best return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations as cb def choose_best_sum(t, k, ls): try: return max ([s for s in [sum(i) for i in cb(ls,k)] if s<=t]) except: return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import numpy as np def number_to_bin_array(a, k, digits): b = bin(a) arr = [] one_count = 0 while a != 0: val = a%2 if val == 1: one_count += 1 if one_count > k: return None arr.append(a%2) a = int(a/2) if one_count < k: return None for i in range(digits - len(arr)): arr.append(0) arr.reverse() return arr def choose_best_sum(t, k, ls): if len (ls) < k: return None ls_f = np.array(ls) ls_f.sort(); min_sum = 0 for i in range(0, k - 1): min_sum += ls_f[i] indices = [] for l in range(0, len(ls_f)): if ls_f[l] > t - min_sum: indices.append(l) ls_f = np.delete(ls_f, indices) if len (ls_f) < k: return None town_count = len(ls_f) m = 1<<town_count max_dist = 0 for mask in range (0, m): n = number_to_bin_array(mask, k, town_count) if n == None: continue sum = 0 for i in range (0, len(n)): if n[i] == 1: sum+=ls_f[i] if sum <= t and sum > max_dist: max_dist = sum if max_dist == 0: return None return max_dist
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): variants=[x for x in combinations(ls,k) if sum(x)<=t] return max(sum(x) for x in variants) if variants!=[] else None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): # your code d = itertools.combinations(ls, k) d = [list(x) for x in d] d = list(k for k,_ in itertools.groupby(d)) d.sort(key=lambda x: sum(x)) res = sum(d[0]) if len(d) > 0 and sum(d[0]) <= t else None for i in range(1, len(d)): if sum(d[i]) > t: break else: res = sum(d[i]) return res
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations,permutations def choose_best_sum(t, k, ls): print(t,k,ls) if k>int(len(ls)): return None new=[] for i in list(combinations(ls,k)): new.append(sum(i)) for i in sorted(new,reverse=True): if i<=t: return i return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): arr = list(itertools.combinations(ls, k)) new_arr = set([sum(item) for item in arr if sum(item) <= t]) return max(new_arr) if len(new_arr) >= 1 else None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): best = (-1, None) # (best total distance travelled, the combination that gives you that) for i in combinations(ls, k): if (sum(i) <= t) and (sum(i) > best[0]): best = (sum(i), i) print(best) if best[0] == -1: return(None) return(best[0])
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools as it def choose_best_sum(t, k, ls): if len(ls) > 0: combs = [sum(i) for i in list(it.combinations(ls, k)) if sum(i) <= t] try: return(max(combs)) except: return(None)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): filtered = list([x for x in ls if x <= t]) mutations = list(itertools.combinations(filtered, k)) if not mutations: return None sums = list([x for x in [sum(m) for m in mutations] if x <= t]) if not sums: return None return max(sums)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): distances = [] for comb in itertools.combinations(ls, k): if sum(comb) <= t: distances.append(sum(comb)) return None if not distances else max(distances)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): ls = [sum(seq) for seq in itertools.combinations(ls,k) if sum(seq) <= t] if ls: return max(ls) else : return None # your code
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): return max((n for n in (sum(v) for v in list(combinations(ls, k))) if n <=t), default=None)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): print((t,k, ls)) ls = [a for a in sorted(ls) if a <= t] if len(ls) < k: return None return _choose_best_sum(t, k, ls) def _choose_best_sum(t, k, ls): if k == 1: w = [a for a in ls if a <= t] if w: return w[-1] else: return None if sum(ls[:k]) > t: return None las_sum = sum(ls[-k:]) if las_sum < t: return las_sum r = None for i in range(1, len(ls) - k + 2): val = ls[-i] if val > t: continue l = ls[:] del l[-i] result = _choose_best_sum(t-val, k-1, l) if result is not None: if r is not None: r = max(r, result + val) else: r = result + val if r is not None: return r return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): ks = list(range(len(ls))) c = list(combinations(ks, k)) best = 0 for choice in c: s = sum(ls[d] for d in choice) if not s > t and s > best: best = s return best or None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): import itertools best = 0 candidates = list(itertools.combinations(ls, k)) for candidate in candidates: if sum(candidate) <= t and sum(candidate) > best: best = sum(candidate) else: continue if best == 0: return None else: return best
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): p=list(ls) a=itertools.combinations(p,k) x=0 c=0 p.sort() if k>len(ls): return None for i in range(k): c=c+p[i] if i+1==len(ls): break if c>t: return None for i in a: if sum(i)<=t and ((t-sum(i)) < (t-x)): x=sum(i) return x
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): import itertools combinations = [sum(i) for i in itertools.combinations(ls,k) if sum(i) <= t] if len(combinations) == 0: return None return max(combinations)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): # your codep print(t, k, ls) posible_roads = itertools.combinations(ls, k) under_posible = [] for posibility in posible_roads: # print(sum(posibility)) if sum(posibility) == t: return t elif sum(posibility) <= t: under_posible.append(sum(posibility)) return sorted(under_posible)[-1] if under_posible else None print(sorted(under_posible))
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): from itertools import combinations ls_ = [sum(x) for x in [*combinations(ls, k)] if sum(x) <= t] if len(ls_) == 0: return None else: return max(ls_)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): l = [i for i in ls if i<=t] print(t, k, ls, l) try: return sorted([sum(i) for i in combinations(l, k) if sum(i)<=t])[-1] except: return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): uniqueCombinations = list(combinations(ls, k)) sumOfCombos = [] for tup in uniqueCombinations: tupSum = sum(list(tup)) if tupSum <= t: sumOfCombos.append(tupSum) if len(sumOfCombos) == 0: return None else: return min(sumOfCombos, key=lambda x:abs(x-t))
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): sum_list = list(set(sum(i) for i in combinations(ls, k))) best = [9999999999, 0] for i in sum_list: if t - i >= 0 and t - i < best[0]: best[0], best[1] = t - i, i if best[1] == 0: return None else: return best[1]
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): if len(ls)<k: return None best_sum = max([sum(c) if sum(c)<=t else 0 for c in combinations(ls, k)]) return best_sum if best_sum else None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): # for i in itertools.combinations(ls,k): # print(i) try: return max(sum(i) for i in itertools.combinations(ls,k) if sum(i)<=t) except: return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations from operator import gt def choose_best_sum(max_, count, distances): sums = list(filter(max_.__ge__, list(map(sum, combinations(distances, count))))) return max(sums) if sums else None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): import itertools sumList = [] c = list(itertools.combinations(ls, k)) #find all combinations without repetition mySet = set(c) #store combinations in unordered set for i in mySet: currentSum = sum(i) if currentSum <= t: sumList.append(currentSum) #appends list if sum being iterated is less than or equal to limit if len(sumList) == 0: return None #checks if there are any possible sums else: return max(sumList) #returns greatest possible sum within given arguments
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): from itertools import combinations i = [sum(a) for a in list(combinations(ls,k))] i.sort() result = None for a in i: if a <= t: result = a return(result)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
combinationTotals = [] def choose_best_sum(t, k, ls): combinationTotals.clear() # your code ls.sort() total = 0 if k > len(ls) or sum(ls[:k]) > t: return None combine_segments(k, ls, [], t) combinationTotals.sort() return combinationTotals[-1] def combine_segments(k, ls, arr, t): for i in range(len(ls)-k+1): arr.append(ls[i]) if k == 1: total = sum(arr) if total < t: combinationTotals.append(total) elif total == t: print(arr) combinationTotals.append(total) return t else: arr.pop() break else: if(combine_segments(k-1, ls[i+1:], arr, t) != None): return t arr.pop() return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): from itertools import combinations c = [i for i in combinations(ls, k)] l = [] for i in c: l.append(sum(i)) return max(list(filter(lambda x: x <= t, l)), default = None)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): from itertools import combinations c = [i for i in combinations(ls, k)] l = [] for i in c: l.append(sum(i)) f = list(filter(lambda x: x <= t, l)) return None if len(f) == 0 else max(f)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
def choose_best_sum(t, k, ls): from itertools import combinations try: return max(s for s in map(sum, combinations(ls, k)) if s <= t) except ValueError: return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): routes = combinations(ls, k) sums = sorted([x for x in list(map(sum, routes)) if x <= t]) if sums: return sums[-1]
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): try: return min((abs(sum(x) - t), sum(x)) for x in combinations(ls, k) if sum(x) - t <= 0)[1] except ValueError: return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): tp = [] for m in combinations(ls, k): if sum(m) <= t: if sum(m) == t: return sum(m) tp.append(sum(m)) return max(tp) if len(tp) > 0 else None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
best_sums = {} def choose_best_sum(t, k, ls): ls = [x for x in ls if x <= t] if k > len(ls): return None if k == 1: best_sum= max(ls) return best_sum ls.sort() the_tuple= tuple([t]+[k]+ls) best_sum = best_sums.get(the_tuple) if best_sum is not None: return best_sum else: i= 0 best_sum = None while i < len(ls): nt = t-ls[i] s = choose_best_sum(nt, k-1, ls[0:i]+ls[i+1:]) if s is None: del ls[i] continue else: best_sum = max(0 if best_sum is None else best_sum, s+ls[i]) if best_sum == t: best_sums[the_tuple]= best_sum return best_sum i += 1 if best_sum is not None: best_sums[the_tuple]= best_sum return best_sum
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): a = sorted([sum(i) for i in combinations(ls, k)]) for i in a[::-1]: if i <= t: return i return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): n = itertools.combinations(ls,k) try: return max([sum(i) for i in n if sum(i) <= t]) except: return None
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
import itertools def choose_best_sum(t, k, ls): result = [sum(seq) for seq in itertools.combinations(ls, k) if sum(seq) <= t] if len(result) == 0: return None else: return max(result)
John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. `ls = [50, 55, 57, 58, 60]`. John is tired of driving and he says to Mary that he doesn't want to drive more than `t = 174 miles` and he will visit only `3` towns. Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible to please Mary and John? Example: With list `ls` and 3 towns to visit they can make a choice between: `[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]`. The sums of distances are then: `162, 163, 165, 165, 167, 168, 170, 172, 173, 175`. The biggest possible sum taking a limit of `174` into account is then `173` and the distances of the `3` corresponding towns is `[55, 58, 60]`. The function `chooseBestSum` (or `choose_best_sum` or ... depending on the language) will take as parameters `t` (maximum sum of distances, integer >= 0), `k` (number of towns to visit, k >= 1) and `ls` (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of `k` distances less than or equal to the given limit `t`, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return `-1`. Examples: `ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163` `xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)` `ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228` Note: don't modify the input list `ls`
from itertools import combinations def choose_best_sum(t, k, ls): return max((sum(i) for i in combinations(ls,k) if sum(i)<= t), default=None) # arr=[] # for index, num in enumerate(ls): # while index+k-1 <= len(ls)-1: # totalSum = 0 # totalSum = sum(ls[index+1:index+k-1],num) # if totalSum < t: # arr.append(totalSum) # index +=1 # print(index,num,totalSum,) # arr.sort() # print(arr) # if arr !=[]: # return arr[-1]
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
MOVES = {"right": (0,1), "down": (1,0), "left": (0,-1), "up": (-1,0)} def get_password(grid, dirs): x,y = next( (x,y) for x,r in enumerate(grid) for y,c in enumerate(r) if c=='x' ) pwd = [] for d in dirs: dx,dy = MOVES[d.strip('T')] x,y = x+dx,y+dy if d.endswith('T'): pwd.append(grid[x][y]) return ''.join(pwd)
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
def get_password(grid, directions): r, c = [(r, c) for r in range(len(grid)) for c in range(len(grid[r])) if grid[r][c] == 'x'].pop() p = '' for d in directions: r, c = (r + {'d':1, 'u':-1}.get(d[0], 0), c + {'r':1, 'l':-1}.get(d[0], 0)) if d[-1] == 'T': p += grid[r][c] return p
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
DIRS = { 'left': (0, -1), 'right': (0, 1), 'up': (-1, 0), 'down': (1, 0), } def get_password(grid, directions): i, j = next((i, j) for i, row in enumerate(grid) for j, x in enumerate(row) if x == 'x') pw = '' for d in directions: take = d.endswith('T') i_delta, j_delta = DIRS[d.rstrip('T')] i += i_delta j += j_delta if take: pw += grid[i][j] return pw
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
def get_password(grid,directions): res = '' for i in range(len(grid)): if 'x' in grid[i]: pos =[i, grid[i].index('x')] for i in directions: if i[0] == 'l': pos[1]-=1 elif i[0] == 'd': pos[0]+=1 elif i[0] == 'r': pos[1]+=1 else: pos[0]-=1 if i[-1] == 'T': res += grid[pos[0]][pos[1]]; return res
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
X, Y = {'l':-1, 'r':1}, {'u':-1, 'd':1} def get_password(grid, directions): result, (i, j) = [], next((i, j) for i,row in enumerate(grid) for j,c in enumerate(row) if c == 'x') for d in directions: i, j = i+Y.get(d[0], 0), j+X.get(d[0], 0) if d[-1] == 'T': result.append(grid[i][j]) return ''.join(result)
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
def get_password(grid,directions): m_go = { 'left':(0,-1), 'right':(0,1), 'up':(-1,0), 'down':(1,0) } x,y = next((i,j) for i, e in enumerate(grid) for j, l in enumerate(e) if l == 'x') password = '' for e in directions: (x_,y_), end = m_go.get(e.strip('T')), e[-1] x, y = x+x_, y+y_ if end is 'T': password += grid[x][y] return password
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
moves = {"l": (-1, 0), "u": (0, -1), "d": (0, 1), "r": (1, 0)} def get_password(grid, directions): pwd, (x, y) = "", next((row.index("x"), j) for j, row in enumerate(grid) if "x" in row) for d in directions: x, y = x + moves[d[0]][0], y + moves[d[0]][1] if d[-1] == "T": pwd = f"{pwd}{grid[y][x]}" return pwd
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
def get_password(grid,directions): axis={'left':-1,'leftT':-1,'right':+1, 'rightT':+1} ordinate={'up':-1,'upT':-1,'down':+1, 'downT':+1} pwd='' for ele in grid: if 'x' in ele: row_index, column_index = grid.index(ele), ele.index('x') break for direction in directions: if 't' in direction: column_index += axis[direction] if 'T' in direction: pwd += grid[row_index][column_index] else: row_index += ordinate[direction] if 'T' in direction: pwd += grid[row_index][column_index] return pwd
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
def get_password(grid,directions): #generate start point start = [[k,x] for k,v in enumerate(grid) for x,y in enumerate(v) if y=='x'][0] curr = start a = '' for i in directions: if i[-1] == 'T': if i == 'leftT': curr[1] -= 1 a+= grid[curr[0]][curr[1]] if i == 'rightT': curr[1] += 1 a+= grid[curr[0]][curr[1]] if i == 'upT': curr[0] -= 1 a+= grid[curr[0]][curr[1]] if i == 'downT': curr[0] += 1 a+= grid[curr[0]][curr[1]] if i[-1] != 'T': if i == 'left': curr[1] -= 1 if i == 'right': curr[1] += 1 if i == 'up': curr[0] -= 1 if i == 'down': curr[0] += 1 return a
In this kata you are expected to recover a scattered password in a (m x n) grid (you'll be given directions of all password pieces in the array) The array will contain pieces of the password to be recovered, you'll get directions on how to get all the the pieces, your initial position in the array will be the character "x". Heres what the array looks like The given directions would consist of `[left, right, up, down]` and `[leftT, rightT, upT, downT]`, the former would be used to move around the grid while the latter would be used when you have a password to that direction of you.( **E.g** if you are in a position and the move to make is `leftT` it means theres a password to the left of you, so take the value and move there) So in the 2d array example above, you will be given the directions `["lefT", "downT", "rightT", "rightT"]`, making for the word `"pa$$"`. Remember you initial position is the character "x". So you write the function `getPassword(grid, directions)` that uses the directions to get a password in the grid. Another example. Once again, Your initial position is the character "x", so from the position of "x" you follow the directions given and get all pieces in the grid.
def get_password(grid,directions): for each in grid: if "x" in each: x = int([grid.index(each)][0]) y = int([each.index("x")][0]) break res = "" for each in directions: if each[0:2] == "ri": y += 1 if each[-1:] == "T": res += grid[x][y] elif each[0:2] == "le": y -= 1 if each[-1:] == "T": res += grid[x][y] elif each[0:2] == "up": x -= 1 if each[-1:] == "T": res += grid[x][y] elif each[0:2] == "do": x += 1 if each[-1:] == "T": res += grid[x][y] return res
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
def sort_nested_list(xsss): ys = iter(sorted(x for xss in xsss for xs in xss for x in xs)) return [[[next(ys) for x in xs] for xs in xss] for xss in xsss]
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
import numpy as np def sort_nested_list(A): return np.sort(A, axis=None).reshape(np.array(A).shape).tolist()
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
def sort_nested_list(array): numbers = iter(sorted(n for a1 in array for a2 in a1 for n in a2)) return [[[next(numbers) for n in a2] for a2 in a1] for a1 in array]
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
def sort_nested_list(A): numbers = [] def peel(A, insert=False): for i in range(len(A)): if len(A[i]) != 0 and isinstance(A[i][0], list): A[i] = peel(A[i], insert) else: if insert: A[i] = numbers[:len(A[i])] del numbers[:len(A[i])] else: numbers.extend(A[i]) return A peel(A) numbers = sorted(numbers) return peel(A, insert=True)
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
import re def sort_nested_list(arr): s=str(arr); ns=sorted(re.findall(r"\d+",s),key=int,reverse=True) return eval(re.sub(r"\d+",lambda _:ns.pop(), s))
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
def sort_nested_list(A, emol = ''): for e in str(A): if e.isdigit() and emol[-1] is '}': continue emol+= [e,'{}'][e.isdigit()] return eval(emol.format(*sorted([ i for l in A for e in l for i in e ])))
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
def fl(l): for x in l: if isinstance(x,list): for j in fl(x): yield j else: yield x def sort_nested_list(a): numbers = iter(sorted(fl(a))) def b(n,a): return [next(n)if isinstance(c,int)else b(n,c)for c in a] return b(numbers,a)
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
from copy import deepcopy def sort_nested_list(a): def seeker(lst): return lst if not lst or not isinstance(lst[0], list) else sum(map(seeker, lst), []) def putter(lst): for i in range(len(lst)): if isinstance(lst[i],list): putter(lst[i]) else: lst[i] = next(elts) a, elts = deepcopy(a), iter(sorted(seeker(a))) putter(a) return a
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
import re def sort_nested_list(A): a=str(A); b,a=a,re.sub('\[|\]','',a); B=str(sorted(eval('['+a+']'))) b,nl=list(re.sub("\d+", "#",b)),re.findall("\d+",B)[::-1] for i in range(len(b)): b[i]= nl.pop() if b[i]=='#' else b[i] return eval(''.join(b))
You get some nested lists. Keeping the original structures, sort only elements (integers) inside of the lists. In other words, sorting the intergers only by swapping their positions. ``` Example Input : [[[2, 1], [4, 3]], [[6, 5], [8, 7]]] Output : [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ``` Note: The structures of the lists are regular (symmetrical) and their depths are 3.
def sort_nested_list(A): sort_nested_list.numbers = [] def collect_numbers(lista): if len(lista) == 0: return lista if len(lista) > 1: for i in lista: try: for e in i: for q in e: sort_nested_list.numbers.append(q) except: return sorted(lista) return lista else: return [collect_numbers(lista[0])] def place_numbers(lista): if len(lista) == 0: return lista if len(lista) > 1: for i in lista: try: for e in i: for index, element in enumerate(e): e[index] = sort_nested_list.numbers.pop(0) except: return sorted(lista) return lista else: return [place_numbers(lista[0])] collect_numbers(A) sort_nested_list.numbers.sort() return place_numbers(A)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): return round(subtotal * ( 1 + tax / 100.0 + tip /100.0), 2)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): return round((1 + (tip + tax) / 100.0) * subtotal, 2)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): tax = tax/100 * subtotal tip = tip/100 * subtotal return round(subtotal + tax + tip, 2)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): return round(subtotal * (100 + tax + tip)/100, 2)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): return float("{0:.2f}".format(subtotal + subtotal*(tax+tip)/100))
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): meal = subtotal*(1+(tax+tip)/100) return round(meal,2)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): return round(subtotal + tax / 100 * subtotal + tip / 100 * subtotal, 2)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def percentage(value, percent): return value * percent / 100 def calculate_total(subtotal, tax, tip): total = subtotal + percentage(subtotal, tax) + percentage(subtotal, tip) return round(total, 2)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): return round(subtotal * (100 + tip + tax) / 100, 2)
Create a function that returns the total of a meal including tip and tax. You should not tip on the tax. You will be given the subtotal, the tax as a percentage and the tip as a percentage. Please round your result to two decimal places.
def calculate_total(subtotal, tax, tip): tax = (tax/100.00) * subtotal tip = (tip/100.00) * subtotal return round(subtotal + tip + tax, 2)
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
from fractions import gcd def count_black_cells(h, w): return (h + w) -2 + gcd(h,w)
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
from math import floor, ceil def count_black_cells(h, w): if w < h: h, w = w, h diag, step = -2, w / h for i in range(h): left = floor(i * step - 1e-9) right = ceil((i+1) * step + 1e-9) diag += right - left return diag
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
from math import gcd def count_black_cells(h, w): return h + w + gcd(h, w) - 2
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
import math def count_black_cells(h, w): return h+w+math.gcd(h,w)-2
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
from fractions import gcd def count_black_cells(h, w): return h + w + gcd(h, w) - 2
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
import math def count_black_cells(h, w): r,y,up,t=0,1,0,None while(y<=w): t=math.ceil(h*y/w) r+=t-up+(1 if y!=w and h*y/w==t else 0) y+=1 up=t-1 return r
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
from fractions import Fraction as F from math import ceil, floor def count_black_cells(h, w): total = 0 r = F(h, w) last_y = 0 for x in range(w + 1): y = x * r total += ceil(y) - floor(last_y) if 0 < x < w and y == int(y): total += 2 last_y = y return total
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
from math import gcd def count_black_cells(h, w): return w + h + gcd(h, w) - 2
# Task Imagine a white rectangular grid of `n` rows and `m` columns divided into two parts by a diagonal line running from the upper left to the lower right corner. Now let's paint the grid in two colors according to the following rules: ``` A cell is painted black if it has at least one point in common with the diagonal; Otherwise, a cell is painted white. ``` Count the number of cells painted black. # Example For n = 3 and m = 4, the output should be `6` There are 6 cells that have at least one common point with the diagonal and therefore are painted black. For n = 3 and m = 3, the output should be `7` 7 cells have at least one common point with the diagonal and are painted black. # Input/Output - `[input]` integer `n` The number of rows. Constraints: 1 ≤ n ≤ 10000. - `[input]` integer `m` The number of columns. Constraints: 1 ≤ m ≤ 10000. - `[output]` an integer The number of black cells.
def count_black_cells(h, w): if h==w: return 3*h-2 r=0 x1=0 for i in range(h): x2=w*(i+1)/h r+=(int(x2)+1)-int(x1) if int(x1)==x1 and x1!=0: r+=1 elif i==h-1: r-=1 x1=x2 return r
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
from collections import Counter def char_freq(message): return Counter(message)
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
from collections import Counter as char_freq
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): result = {} for letter in message: result[letter] = result.get(letter, 0) + 1 return result
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): return { ch : message.count(ch) for ch in message }
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): f = {} for char in list(message): if char in f: f[char] += 1 else: f[char] = 1 return f
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): return { char: message.count(char) for char in message }
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): chars = {} for char in message: chars[char] = chars.get(char, 0) + 1 return chars
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): return {ch: message.count(ch) for ch in set(message)}
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): chars, counts = list(), list() for c in message: if c not in chars: chars.append(c) counts.append(message.count(c)) return dict(zip(chars, counts))
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): frequencies = {} for char in message: if char in frequencies: frequencies[char] += 1 else: frequencies[char] = 1 return frequencies
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): return { e:message.count(e) for e in message }
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): dict = {} for e in message: keys = dict.keys() if e in keys: dict[e] += 1 else: dict[e] = 1 return dict
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): pass text = list(message) dic = {} for x in message: dic[x] = text.count(x) return dic
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
from collections import defaultdict def char_freq(message): res = defaultdict(int) for c in message: res[c] += 1 return res
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
import collections char_freq = collections.Counter
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): return {x:message.count(x) for x in list(message)}
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(message): b={} for a in message: b.setdefault(a,0) b[a]=b[a]+1 return b
## Description Welcome, Warrior! In this kata, you will get a message and you will need to get the frequency of each and every character! ## Explanation Your function will be called `char_freq`/`charFreq`/`CharFreq` and you will get passed a string, you will then return a dictionary (object in JavaScript) with as keys the characters, and as values how many times that character is in the string. You can assume you will be given valid input. ## Example ```python char_freq("I like cats") // Returns {'a': 1, ' ': 2, 'c': 1, 'e': 1, 'I': 1, 'k': 1, 'l': 1, 'i': 1, 's': 1, 't': 1} ```
def char_freq(m): d = dict() for i in m: d[i] = m.count(i) return d