Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): # your code lst = strng.split('\n') return '\n'.join(i[::-1] for i in lst) def hor_mirror(strng): lst = strng.split('\n') return '\n'.join(lst[::-1]) def oper(fct, s): return vert_mirror(s) if fct == vert_mirror else hor_mirror(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): return '\n'.join(elem[::-1] for elem in strng.split('\n')) def hor_mirror(strng): return '\n'.join(strng.split('\n')[::-1]) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(s): return "\n".join(["".join([line[i] for i in range(len(line)-1,-1,-1)]) for line in s.splitlines()]) def hor_mirror(s): return "\n".join([s.splitlines()[i] for i in range(len(s.splitlines())-1,-1,-1)]) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(parts): return [p[::-1] for p in parts] def hor_mirror(parts): return parts[::-1] def oper(fct, s): parts = s.split('\n') return '\n'.join(fct(parts))
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): return "\n".join([e[::-1] for e in strng.split()]) def hor_mirror(strng): return "\n".join([e for e in strng.split()[::-1]]) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): return "".join([e[::-1]+"\n" for e in strng.split()])[0:-1] def hor_mirror(strng): return "".join([e+"\n" for e in strng.split()[::-1]])[0:-1] def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): return '\n'.join([ row[::-1] for row in strng.split('\n') ]) def hor_mirror(strng): return '\n'.join([ row for row in reversed(strng.split('\n')) ]) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): return '\n'.join([x[::-1] for x in strng.split('\n')]) def hor_mirror(strng): return '\n'.join([x for x in strng.split('\n')[::-1]]) def oper(fct, s): if fct == vert_mirror: return vert_mirror(s) elif fct == hor_mirror: return hor_mirror(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): return '\n'.join([s[::-1] for s in strng.split('\n')]) def hor_mirror(strng): return '\n'.join([s for s in strng.split('\n')[::-1]]) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
vert_mirror = lambda x:'\n'.join([i[::-1] for i in x.split('\n')]) hor_mirror = lambda x: '\n'.join(x.split('\n')[::-1]) oper = lambda x, y: x(y)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): subStrings = strng.split('\n') reversedStrings = [substring[::-1] for substring in subStrings] joinedStrings = '\n'.join(reversedStrings) return joinedStrings def hor_mirror(strng): subStrings = strng.split('\n') reversedStrings = [substring for substring in reversed(subStrings)] joinedStrings = '\n'.join(reversedStrings) return joinedStrings def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(string): return '\n'.join(s[::-1] for s in string.split('\n')) def hor_mirror(string): return '\n'.join(string.split('\n')[::-1]) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): a = strng.split("\n") vert_m = '' for word in a: vert_m +=word[::-1] +"\n" return vert_m.strip("\n") def hor_mirror(strng): a = strng.split("\n") a.reverse() hor_m = '' for word in a: hor_m += word + "\n" return hor_m.strip("\n") def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(s): return "\n".join([w[::-1] for w in s.split("\n")]) def hor_mirror(s): return "\n".join([w for w in s.split("\n")][::-1]) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
n="\n" def vert_mirror(s): return n.join([elem[::-1] for elem in s.split(n)]) def hor_mirror(s): return n.join(s.split(n)[::-1]) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(s): splitted_s = s.split('\n') l = [] for word in splitted_s: reversed_word = word[::-1] l.append(reversed_word) new_s = '\n'.join(l) return(new_s) def hor_mirror(s): splitted_s = s.split('\n') splitted_s.reverse() return('\n'.join(splitted_s)) def oper(fct, s): return(fct(s))
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(s): return '\n'.join(_[::-1] for _ in s.split()) def hor_mirror(s): return '\n'.join(s.split()[::-1]) def oper(fct, s): return fct(s) # Flez
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(s): s = s.splitlines() for i in range(len(s)): s[i] = s[i][::-1] s = '\n'.join(s) return s def hor_mirror(s): s = s[::-1].splitlines() for i in range(len(s)): s[i] = s[i][::-1] s = '\n'.join(s) return s def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(string): return "\n".join(row[::-1] for row in string.split("\n")) def hor_mirror(string): return "\n".join(string.split("\n")[::-1]) def oper(fct, string): return fct(string)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): a = strng.split("\n") b = [i[::-1] for i in a] return "\n".join(b) def hor_mirror(strng): a = strng.split("\n") b = a[::-1] return "\n".join(b) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): strng_list = strng.split('\n') for i in range(len(strng_list)): strng_list[i] = strng_list[i][::-1] return "\n".join(strng_list) def hor_mirror(strng): strng_list = strng.split('\n') strng_list.reverse() return "\n".join(strng_list) def oper(fct, s): return fct(s)
This kata is the first of a sequence of four about "Squared Strings". You are given a string of `n` lines, each substring being `n` characters long: For example: `s = "abcd\nefgh\nijkl\nmnop"` We will study some transformations of this square of strings. - Vertical mirror: vert_mirror (or vertMirror or vert-mirror) ``` vert_mirror(s) => "dcba\nhgfe\nlkji\nponm" ``` - Horizontal mirror: hor_mirror (or horMirror or hor-mirror) ``` hor_mirror(s) => "mnop\nijkl\nefgh\nabcd" ``` or printed: ``` vertical mirror |horizontal mirror abcd --> dcba |abcd --> mnop efgh hgfe |efgh ijkl ijkl lkji |ijkl efgh mnop ponm |mnop abcd ``` # Task: - Write these two functions and - high-order function `oper(fct, s)` where - fct is the function of one variable f to apply to the string `s` (fct will be one of `vertMirror, horMirror`) # Examples: ``` s = "abcd\nefgh\nijkl\nmnop" oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm" oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd" ``` # Note: The form of the parameter `fct` in oper changes according to the language. You can see each form according to the language in "Sample Tests". # Bash Note: The input strings are separated by `,` instead of `\n`. The output strings should be separated by `\r` instead of `\n`. See "Sample Tests". Forthcoming katas will study other transformations.
def vert_mirror(strng): return '\n'.join([r[::-1] for r in strng.split('\n')]) def hor_mirror(strng): return '\n'.join([r for r in strng.split('\n')][::-1]) def oper(fct, s): return fct(s)
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
def adjust(coin, price): return price + (coin - price) % coin
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
from math import ceil adjust = lambda coin, price: ceil(price / float(coin)) * coin
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
def adjust(coin, price): q, r = divmod(price, coin) return price if not r else (q + 1) * coin
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
from math import ceil def adjust(a, b): return ceil(float(b) / a) * a
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
def adjust(coin, price): residue = price % coin if residue: price += coin - residue return price # fix me!
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
def adjust(coin, price): if( price%coin !=0): price += coin - price%coin return price
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
adjust=lambda b,n:n+-n%b
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
def adjust(coin, price): if(price%coin==0): return price else: return price+coin-price%coin
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
def adjust(coin, price): return price / coin * coin + bool(price % coin) * coin
Inspired by [Round to the next 5](/kata/55d1d6d5955ec6365400006d). Warning! This kata contains spoilers on the mentioned one. Solve that one first! # The Coins of Ter Ter is a small country, located between Brelnam and the Orange juice ocean. It uses many different coins and bills for payment. However, one day, the leaders of Ter decide that there are too many small coins. Therefore, they ban the small coins. But no one knows _which_ coins they'll ban, so they ask you to provide a tool that can recalculate a price. After all, if one does not have a 1 Terrek bill and can only give a 2 Terrek bill, one needs to adjust the oddly priced items. # Task Write a function `adjust`, that takes a two integers: the lowest currency unit that's still allowed, and the price/debt that needs to get adjusted. All prices are going up, and debts are remitted. The lowest currency will always be positive. In other words:`adjust` takes two integers, `b` and `n`, and returns the smallest number `k`, such that `n <= k` and `k % b == 0 `. # Examples ```python adjust( 3, 0 ) == 0 adjust( 3, 1 ) == 3 adjust( 3, -2) == 0 adjust( 3, -4) == -3 adjust( 3, 3 ) == 3 adjust( 3, 6 ) == 6 adjust( 3, 7 ) == 9 ``` Translator notice: Make sure that you provide about the same random tests, so that a user can get feedback during "Run tests", and not only during "Submit".
def adjust(coin, price): if price % coin == 0: return price else: return (price//coin + 1) * coin
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
def square_up(n): return [j if j <= i else 0 for i in range(1, n+1) for j in range(n, 0, -1)]
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
def square_up(n): result = [] for i in range(1, n + 1): result.extend([0] * (n - i) + list(range(i, 0, -1))) return result
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
def square_up(n): lst = [] max = 1 while max <=n: for x in range(n,0,-1): if x-max < 1: lst.append(x) else: lst.append(0) max+=1 return lst
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
from itertools import chain, repeat def square_up(n): return list(chain.from_iterable( chain(repeat(0, n-i), range(i, 0, -1)) for i in range(1, n+1) ))
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
def square_up(n): x = list(range(n,0,-1)) y = [ ([0] * i + x[i:]) for i in range(n-1,-1,-1) ] return [i for l in y for i in l]
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
def square_up(n): sqrList = [0 for _ in range(n*n)] #we build the square list, from right to left, in sets # e.g. for n==5 [5i-1, 5i-2, 5i-3, 5i-4, 5i-5] for i = 1,2,...,n for i in range(1,n+1): #iterate over the 1,2,...,n sets index = n*i - 1 #Start the index in the right most index of the set n for j in range(1, i+1): #pattern: 1, 1;2, 1;2;3, 1;2;3;4, ..., 1;2;3;;;n sqrList[index] = j index -=1 return sqrList #-----end function
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
def square_up(n): a = [] for j in range(1,n+1): for i in range(n, j, -1): a.append(0) for i in range(j, 0, -1): a.append(i) return a
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
def square_up(n): """ Codewars 练习题: Array - squareUp b! :param n: :return: """ ans = [] for i in range(1, n + 1): for k in range(n - i): ans.append(0) for j in range(i, 0, -1): ans.append(j) return ans
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
def square_up(n): return [n - j if n - j - 1 <= i else 0 for i in range(n) for j in range(n)]
This is a question from codingbat Given an integer n greater than or equal to 0, create and return an array with the following pattern: squareUp(3) => [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) => [0, 1, 2, 1] squareUp(4) => [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] n<=1000. # Check out my other kata! Matrix Diagonal Sort OMG String -> N iterations -> String String -> X iterations -> String ANTISTRING Array - squareUp b! Matrix - squareUp b! Infinitely Nested Radical Expressions pipi Numbers!
square_up=lambda n:sum([[0]*(i-1)+list(range(n,0,-1))[i-1:] for i in range(n,0,-1)], [])
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
def j_n(n): j = [0] a = [1] for i in range(1, n): j.append((i - a[j[i-1]])) a.append((i-j[a[i-1]])) return j, a def john(n): return j_n(n)[0] def ann(n): return j_n(n)[1] def sum_john(n): return sum(john(n)) def sum_ann(n): return sum(ann(n))
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
from functools import lru_cache @lru_cache(maxsize = None) def john_value(n): if n == 0: return 0 return n - ann_value(john_value(n - 1)) @lru_cache(maxsize = None) def ann_value(n): if n == 0: return 1 return n - john_value(ann_value(n - 1)) def ann(n): return [ann_value(x) for x in range(n)] def john(n): return [john_value(x) for x in range(n)] def sum_john(n): return sum(john(n)) def sum_ann(n): return sum(ann(n))
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
def get_arrays(n, name): a = [1] j = [0] for i in xrange(1, n): next_j = i - a[j[i-1]] j.append(next_j) next_a = i - j[a[i-1]] a.append(next_a) ret = { 'john': j, 'ann' : a } return ret[name] def john(n): return get_arrays(n, 'john') def ann(n): return get_arrays(n, 'ann') def sum_john(n): return sum(john(n)) def sum_ann(n): return sum(ann(n))
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
an, jh = [1], [0] for i in range(1,int(1e5*5)): jh.append(i-an[jh[-1]]) an.append(i-jh[an[-1]]) john=lambda n:jh[:n] ann=lambda n:an[:n] sum_john=lambda n:sum(jh[:n]) sum_ann=lambda n:sum(an[:n])
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
from functools import lru_cache @lru_cache(maxsize=None) def j(n): return n - a(j(n-1)) if n else 0 @lru_cache(maxsize=None) def a(n): return n - j(a(n-1)) if n else 1 def john(n): return [j(i) for i in range(n)] def ann(n): return [a(i) for i in range(n)] def sum_john(n): return sum(john(n)) def sum_ann(n): return sum(ann(n))
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
def generate_solved_katas(n): """ 生成ann和john在第n天时的解题数量表""" john_katas = [0] ann_katas = [1] for i in range(1, n): john_katas.append(i - ann_katas[john_katas[i-1]]) ann_katas.append(i - john_katas[ann_katas[i-1]]) return john_katas, ann_katas def john(n): return generate_solved_katas(n)[0] def ann(n): return generate_solved_katas(n)[1] def sum_john(n): return sum(john(n)) def sum_ann(n): return sum(ann(n))
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
def john_and_ann(n): john = [0] ann = [1] for i in range(1, n): t1, t2 = ann[i-1], john[i-1] john.append(i - ann[t2]) ann.append(i - john[t1]) return john, ann def ann(n): john, ann = john_and_ann(n) return ann def john(n): john, ann = john_and_ann(n) return john def sum_ann(n): Ann = ann(n) return sum(Ann) def sum_john(n): John = john(n) return sum(John)
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
jm, am = {0:0}, {0:1} def j(n): if n in jm.keys(): return jm[n] jm[n] = n - a(j(n-1)) return jm[n] def a(n): if n in am.keys(): return am[n] am[n] = n - j(a(n-1)) return am[n] def john(n): return [j(i) for i in range(n)] def ann(n): return [a(i) for i in range(n)] def sum_john(n): return sum(john(n)) def sum_ann(n): return sum(ann(n))
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
#Dictionaries to cache results john_dict = {} ann_dict = {} #Helper functions using caches to find each value. #I couldn't find a way to simplify this without making it more complex. def john_val(n): if n == 0: return 0 if not n in john_dict: john_dict[n] = n - ann_val(john_val(n-1)) return john_dict[n] def ann_val(n): if n == 0: return 1 if not n in ann_dict: ann_dict[n] = n - john_val(ann_val(n-1)) return ann_dict[n] #Once the helpers are done it's easy def john(n): return [john_val(i) for i in range(n)] def ann(n): return [ann_val(i) for i in range(n)] def sum_john(n): return sum(john(n)) def sum_ann(n): return sum(ann(n))
John and his wife Ann have decided to go to Codewars. On first day Ann will do one kata and John - he wants to know how it is working - 0 kata. Let us call `a(n)` - and `j(n)` - the number of katas done by Ann - and John - at day `n`. We have `a(0) = 1` and in the same manner `j(0) = 0`. They have chosen the following rules: - On day `n` the number of katas done by Ann should be `n` minus the number of katas done by John at day `t`, `t` being equal to the number of katas done by Ann herself at day `n - 1`. - On day `n` the number of katas done by John should be `n` minus the number of katas done by Ann at day `t`, `t` being equal to the number of katas done by John himself at day `n - 1`. Whoops! I think they need to lay out a little clearer exactly what there're getting themselves into! # Could you write: - 1) two functions `ann` and `john (parameter n)` giving the list of the numbers of katas Ann and John should take on the first n days (see first examples below)? - 2) The total number of katas taken by ann `function sum_ann(n)` and john `function sum_john(n)` - on the first n days? The functions in 1) are not tested in Fortran and not tested in Shell. # Examples: ``` john(11) --> [0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6] ann(6) --> [1, 1, 2, 2, 3, 3] sum_john(75) --> 1720 sum_ann(150) --> 6930 ``` # Shell Note: sumJohnAndAnn has two parameters: first one : n (number of days, $1) second one : which($2) -> - 1 for getting John's sum - 2 for getting Ann's sum. See "Sample Tests". # Note: Keep an eye on performance.
def johnann(n, jnota): j, a = [0], [1] for i in range(1,n): j.append(i-a[j[-1]]) a.append(i-j[a[-1]]) return j if jnota else a john = lambda n: johnann(n, True) ann = lambda n: johnann(n, False) sum_john = lambda n: sum(john(n)) sum_ann = lambda n: sum(ann(n))
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from itertools import permutations def ssc_forperm(arr): perms = set(p for p in permutations(arr)) values = [sum((x + 1) * y for x,y in enumerate(i)) for i in perms] return [{"total perm": len(perms)}, {"total ssc": sum(values)}, {"max ssc": max(values)}, {"min ssc": min(values)}]
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from collections import Counter from math import factorial import operator def ssc_forperm(xs): n = len(xs) p = factorial(n) // reduce(operator.mul, map(factorial, Counter(xs).itervalues())) sxs = sorted(xs) return [ {"total perm": p}, {"total ssc": (n + 1) * p * sum(xs) // 2}, {"max ssc": sum(i * x for (i, x) in enumerate(sxs, 1))}, {"min ssc": sum(i * x for (i, x) in enumerate(reversed(sxs), 1))}]
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
def ssc_forperm(arr): from itertools import permutations, count, imap from operator import mul perms = list(set(permutations(arr))) scc = [sum(imap(mul, i, count(1))) for i in perms] return [{"total perm": len(scc)}, {"total ssc": sum(scc)}, {"max ssc": max(scc)}, {"min ssc": min(scc)}]
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from math import factorial as f; ssc_forperm=lambda arr: (lambda fac: [{"total perm": fac}, {"total ssc": fac*(len(arr)+1)/2*sum(arr)}, {"max ssc": sum([(i+1)*a for i,a in enumerate(sorted(arr))])}, {"min ssc": sum([(i+1)*a for i,a in enumerate(sorted(arr, reverse=True))])}])(f(len(arr))/reduce(lambda a,b: a*b, [f(arr.count(x)) for x in set(arr)])) from functools import reduce #adjusted to work also in case of 3 similar numbers or more
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from math import factorial as f; ssc_forperm=lambda arr: (lambda fac: [{"total perm": fac}, {"total ssc": fac*(len(arr)+1)/2*sum(arr)}, {"max ssc": sum([(i+1)*a for i,a in enumerate(sorted(arr))])}, {"min ssc": sum([(i+1)*a for i,a in enumerate(sorted(arr, reverse=True))])}])(f(len(arr))/reduce(lambda a,b: a*b, [arr.count(x) for x in set(arr)]))
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from math import factorial as fact from collections import Counter from functools import reduce def ssc_forperm(arr): cnt = Counter(arr) nPerms = fact(len(arr)) // reduce(int.__mul__, map(fact, cnt.values()), 1) sIdx = -~len(arr) * len(arr) >> 1 arr = sorted(arr) return [{"total perm": nPerms}, {"total ssc": sum(v * sIdx * nPerms // len(arr) for v in arr)}, {"max ssc": sum(v*i for i,v in enumerate(arr,1))}, {"min ssc": sum(v*i for i,v in enumerate(reversed(arr),1))} ]
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from itertools import permutations def ssc_forperm(lst): scores = [sum(i*n for i, n in enumerate(p, 1)) for p in set(permutations(lst))] return [{"total perm": len(scores)}, {"total ssc": sum(scores)}, {"max ssc": max(scores)}, {"min ssc": min(scores)}]
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from collections import Counter from itertools import permutations from math import factorial from operator import mul def ssc(xs): return sum(i*x for i, x in enumerate(xs, 1)) def ssc_forperm(arr): m = reduce(mul, (factorial(x) for x in Counter(arr).values())) xs = [ssc(xs) for xs in permutations(arr)] return [{"total perm":len(xs) // m}, {"total ssc": sum(xs) // m}, {"max ssc": max(xs)}, {"min ssc": min(xs)}]
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from itertools import permutations def ssc_forperm(arr): lst = [sum(n*(i+1) for i, n in enumerate(p)) for p in set(permutations(arr))] return [{"total perm": len(lst)}, {"total ssc": sum(lst)}, {"max ssc": max(lst)}, {"min ssc": min(lst)}]
The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. E.g.: with the array ```[6, 12, -1]``` ``` arr = [6, 12, -1 ] ssc = 1*6 + 2* 12 + 3.(*1) = 6 + 24 - 3 = 27 ``` The array given in the example has six(6) permutations and are with the corresponding ssc: ``` Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 ``` The total sum of the ```ssc's``` of all the possible permutations is: ```27 + 40 + 47 + 41 + 28 + 21 = 204``` The maximum value for the ```ssc``` is ```47```. The minimum value for the ```ssc``` is ```21```. We need a special function ```ssc_forperm()``` that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: ``` [{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc": __}] ``` For the example we have above will be: ```python ssc_forperm([6, 12, -1]) = [{"total perm":6}, {"total ssc:204}, {"max ssc":47}, {"min ssc":21}] ``` You may assume that you will never receive an empty array. Also you will never receive an array with the same element in all the positions like [1, 1, 1, 1 ,1], but you may have elements occuring twice or more like [1, 1, 1, 2, 3] Enjoy it!!
from itertools import permutations def ssc_forperm(arr): maximum = -999999999 minimum = 999999999 total_score= [{"total perm":0},{"total ssc":0},{"max ssc":0},{"min ssc":0}] lend = len(set(list(permutations(arr)))) permutated_list = list(set((permutations(arr)))) for element in permutated_list: temp = 0 for e,i in enumerate(element,start = 1): temp += e * i total_score[1]["total ssc"] += temp if temp > maximum: maximum = temp total_score[2]["max ssc"] = maximum if temp < minimum: minimum = temp total_score[3]["min ssc"] = minimum total_score[0]["total perm"] = lend return total_score
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def amidakuji(ar): numbers = list(range(len(ar[0])+1)) for line in ar: for i, swap in enumerate(line): if swap == '1': numbers[i], numbers[i+1] = numbers[i+1], numbers[i] return numbers
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def amidakuji(ar): #your code goes here. you can do it! # uncomment the line below to view the structure of the test case # print(visualizer(ar)) width = len(ar[0]) current = [i for i in range(width + 1)] for i in ar: layer = i count = 0 for s in layer: if s == "1": current = swap(current, count, count + 1) count += 1 return current def swap(l, pos1, pos2): l[pos1], l[pos2] = l[pos2], l[pos1] return l
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def amidakuji(ar): res = [0 for i in range(len(ar[0]) + 1)] for i in range(len(ar[0]) + 1): pos = i for j in range(len(ar)): if pos != 0 and ar[j][pos - 1] == "1": pos -= 1 elif pos != len(ar[0]) and ar[j][pos] == "1": pos += 1 res[pos] = i print(visualizer(ar)) return res
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def amidakuji(ar): # Set up original number list nums = list(range(len(ar[0])+1)) # For each layer: for layer in ar: # Swap the positions of numbers at each rung for pos in range(len(ar[0])): if layer[pos]=="1":nums[pos],nums[pos+1]=nums[pos+1],nums[pos] return nums
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def amidakuji(arr): elements = list(range(len(arr[0]) + 1)) for row in arr: for i, col in enumerate(row): if col == "1": elements[i], elements[i+1] = elements[i+1], elements[i] return elements
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def amidakuji(board): li = [0] * (len(board[0]) + 1) for i in range(len(board[0]) + 1): c = i for k in range(len(board)): if int(board[k][c]) if c<len(board[0]) else 0 : c += 1 elif int(board[k][c - 1]) if c>0 else 0 : c -= 1 li[c] = i return li
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def f(ar, col): for row in ar: if col < len(row) and row[col] == '1': col += 1 elif col > 0 and row[col-1] == '1': col -= 1 return col def amidakuji(ar): return [f(ar[::-1], col) for col in range(len(ar[0]) + 1)]
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
from functools import reduce def amidakuji(ar): *ints, = range(1 + len(ar[0])) return reduce(swap, ar, ints) def swap(acc, rungs): for r, rung in enumerate(rungs): if rung == '1': acc[r : r + 2] = acc[r + 1], acc[r] return acc
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def amidakuji(ar): results = [0 for i in range(len(ar[0]) + 1)] for i in range(len(ar[0]) + 1): pos = i for line in ar: move = False if pos > 0: if line[pos - 1] == '1': pos -= 1 move = True if pos < len(line) and not move: if line[pos] == '1': pos += 1 results[pos] = i return results
Amidakuji is a method of lottery designed to create random pairings between two sets comprised of an equal number of elements. Your task is to write a function amidakuji that returns the final positions of each element. Note that the elements are an ascending sequence of consecutive integers starting with 0 (from left to right). Input Your function will receive an array/list of equal-length strings consisting of 0 and 1 characters; this represents the "ladder" structure. The 1s represent the rungs of the ladder and the 0s represent empty space. Each element begins at the top of its corresponding vertical rail, as illustrated in the diagram below.During the descent of the ladder, whenever a vertical rail intersects a horizontal rung, it swaps values with the adjacent connecting vertical rail. Output Your function should return an array of integers, with each integer in its final position. Test Example The diagram above is a visual representation of the test example below. The yellow highlighted path shows the path taken by the 2 value. Each time it encounters a crosspiece, it shifts position. ```python ladder = [ '001001', '010000', '100100', '001000', '100101', '010010', '101001', '010100' ] amidakuji(ladder) # [4, 2, 0, 5, 3, 6, 1] ``` Other Technical Details A function visualizer is preloaded to help illustrate the structure of the ladder; you can call this function with test inputs No two rungs will ever be adjacent (so there is no ambiguity about directional path) Full Test Suite: 10 fixed tests and 100 randomly-generated tests Test input dimension upper bounds: maximum width: 20 maximum height: 50 Inputs will always be valid If you enjoyed this kata, be sure to check out my other katas
def amidakuji(ar): elements = list(range(len(ar[0]) + 1)) for row in ar: for i, col in enumerate(row): if col == "1": elements[i], elements[i+1] = elements[i+1], elements[i] return elements
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(n): k = 1 while k*(k+1)*(2*k+1)/6 < n: k+=1 return k-1
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(n): s,i = 0,0 while s < n: i += 1 s += i**2 return i-1
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(n): sm = 0 i = 0 while n > sm: i += 1 sm += i ** 2 return i - 1
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
get_number_of_squares=g=lambda n,i=1:i*i<n and-~g(n-i*i,i+1)
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(limit): total = 1 for n in range(2, limit+2): if total >= limit: return n - 2 total += n**2
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(n): i, j, k = 1, 1, 1 while k < n: i += 2 j += i k += j return i >> 1
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(n): s = 0 i = 0 while s < n: i += 1 s += i * i return i - 1
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(n): sum=0 c = 0 for i in range(1, n+1): sum+=i*i c+=1 if sum>=n: return c-1 return 0
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(n): res, i = 0, 0 while res < n: res += i**2 i += 1 return i - 2
Write a function getNumberOfSquares that will return how many integer (starting from 1, 2...) numbers raised to power of 2 and then summed up are less than some number given as a parameter. E.g 1: For n = 6 result should be 2 because 1^2 + 2^2 = 1 + 4 = 5 and 5 < 6 E.g 2: For n = 15 result should be 3 because 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 and 14 < 15
def get_number_of_squares(n): return int((n * 3) ** (1. / 3.) - 0.5)
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(cat, room): (cy, cx), h, w = cat, len(room), len(room[0]) if not (0 <= cy < h and 0 <= cx < w): return "NoCat" ty, tx = next(((y, x) for y in range(h) for x in range(w) if room[y][x]), (-1, -1)) if ty < 0: return "NoTable" ver, dy = "U" if ty < cy else "D", abs(ty - cy) hor, dx = "L" if tx < cx else "R", abs(tx - cx) return f"{hor * dx}{ver * dy}"
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(cat, room): try: ci, cj = cat if not (0 <= ci < len(room) and 0 <= cj < len(room[0])): return "NoCat" ti, tj = next((i, j) for i,row in enumerate(room) for j,x in enumerate(row) if x) return "{}{}".format("UD"[ti>ci]*abs(ti-ci), "LR"[tj>cj]*abs(tj-cj)) except StopIteration: return "NoTable"
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(pos, c): if not (0<=pos[0]<len(c) and 0<=pos[1]<len(c[0])) : return "NoCat" find_one = next(([i, j.index(1)] for i, j in enumerate(c) if 1 in j),None) if not find_one : return "NoTable" f = ("D" if pos[0] < find_one[0] else "U") * abs(pos[0] - find_one[0]) s = ("R" if pos[1] < find_one[1] else "L") * abs(pos[1] - find_one[1]) return f + s
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(cat, room): if cat[0] < 0 or cat[0] >= len(room) or cat[1] < 0 or cat[1] >= len(room[0]): return "NoCat" tableCoord = [-1, -1] for row in range(len(room)): for col in range(len(room[row])): if room[row][col]==1: tableCoord[0] = row tableCoord[1] = col if tableCoord == [-1, -1]: return "NoTable" if cat == tableCoord: return "" move = [0, 0] move[0] = tableCoord[0] - cat[0] move[1] = tableCoord[1] - cat[1] retStr = "" while move != [0, 0]: if move[0] > 0: retStr += "D" move[0] -= 1 if move[0] < 0: retStr += "U" move[0] += 1 if move[1] > 0: retStr += "R" move[1] -= 1 if move[1] < 0: retStr += "L" move[1] += 1 return retStr
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(cat, room): nr, nc = len(room), len(room[0]) cr, cc = cat if not (cr in range(nr) and cc in range(nc)): return 'NoCat' try: tr, tc = next((r, c) for r in range(nr) for c in range(nc) if room[r][c]) except StopIteration: return 'NoTable' dr, dc = tr - cr, tc - cc return 'D' * dr + 'U' * -dr + 'R' * dc + 'L' * -dc
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(a, b): cx, cy = a if not (0 <= cx < len(b) and 0 <= cy < len(b[0])): return "NoCat" if not any(any(x) for x in b): return "NoTable" tx, ty = next((i, j) for i, x in enumerate(b) for j, y in enumerate(x) if y) return "L" * (cy - ty) + "R" * (ty - cy) + "U" * (cx - tx) + "D" * (tx - cx)
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(cat, room): ty,tx = next(((i,j) for i,row in enumerate(room) for j,v in enumerate(row) if v==1),('No','Table')) cy,cx = cat if cy<0 or cx<0 or cy+1>len(room) or cx+1>len(room[0]): return 'NoCat' if ty+tx == 'NoTable': return ty+tx return ('U' if (ty-cy)<0 else 'D')*(abs(ty-cy)) + ('L' if (tx-cx)<0 else 'R')*(abs(tx-cx))
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(cat, room): if not (0 <= cat[0] < len(room) and 0 <= cat[1] < len(room[0])): return 'NoCat' t = ([(y, x) for y, row in enumerate(room) for x, col in enumerate(row) if col == 1] + [None])[0] if not t: return 'NoTable' h = 'L' * abs(t[1] - cat[1]) if t[1] - cat[1] < 0 else 'R' * (t[1] - cat[1]) return h + ('U' * abs(t[0] - cat[0]) if t[0] - cat[0] < 0 else 'D' * (t[0] - cat[0]))
The cat wants to lay down on the table, but the problem is that we don't know where it is in the room! You'll get in input: - the cat coordinates as a list of length 2, with the row on the map and the column on the map. - the map of the room as a list of lists where every element can be 0 if empty or 1 if is the table (there can be only one 1 in the room / lists will never be empty). # Task: You'll need to return the route to the table, beginning from the cat's starting coordinates, as a String. The route must be a sequence of letters U, D, R, L that means Up, Down, Right, Left. The order of the letters in the output isn't important. # Outputs: * The route for the cat to reach the table, as a string * If there isn't a table in the room you must return "NoTable" * if the cat is outside of the room you must return "NoCat" (this output has the precedence on the above one). * If the cat is alredy on the table, you can return an empty String. # Example: ``` cat = [0,1] room =[[0,0,0], [0,0,0], [0,0,1]] The route will be "RDD", or "DRD" or "DDR" ```
def put_the_cat_on_the_table(cat, room): if not (0 <= cat[0] < len(room) and 0 <= cat[1] < len(room[0])): return 'NoCat' aim = None for row, rows in enumerate(room): for col, place in enumerate(rows): if place == 1: aim = (row, col) break if not aim: return 'NoTable' else: route = cat[0] - aim[0], cat[1] - aim[1] if cat == aim: return '' else: return ('U' * route[0] if route[0] >= 0 else 'D' * abs(route[0])) + ( 'L' * route[1] if route[1] >= 0 else 'R' * abs(route[1]))
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(arr): m=arr.index(min(arr)) return sorted(arr) == ( arr[m:]+arr[:m] )
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(arr): return sum(x > y for x,y in zip(arr, arr[1:]+[arr[0]])) < 2
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(arr): flag= 0 for i in range(0, len(arr)): prev = arr[i-1] if prev > arr[i]: flag +=1 if flag > 1: return False else: return True
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(lst): std = sorted(lst) i = len(std) - 1 - std[::-1].index(lst[0]) return std[i:] + std[:i] == lst
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(arr): i = len(arr) - arr[::-1].index(min(arr)) - 1 return arr[i:] + arr[:i] == sorted(arr)
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(arr): N, jump = len(arr), False for i, n in enumerate(arr): if n > arr[(i + 1) % N]: if jump: return False jump = True return True
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(arr): diff = sum(arr[i] > arr[i + 1] for i in range(len(arr) - 1)) return (diff == 1 and arr[-1] <= arr[0]) or diff == 0
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(arr): while arr[-1] <= arr[0]: arr = [arr.pop()] + arr return arr == sorted(arr)
An array is **circularly sorted** if the elements are sorted in ascending order, but displaced, or rotated, by any number of steps. Complete the function/method that determines if the given array of integers is circularly sorted. ## Examples These arrays are circularly sorted (`true`): ``` [2, 3, 4, 5, 0, 1] --> [0, 1] + [2, 3, 4, 5] [4, 5, 6, 9, 1] --> [1] + [4, 5, 6, 9] [10, 11, 6, 7, 9] --> [6, 7, 9] + [10, 11] [1, 2, 3, 4, 5] --> [1, 2, 3, 4, 5] [5, 7, 43, 987, -9, 0] --> [-9, 0] + [5, 7, 43, 987] [1, 2, 3, 4, 1] --> [1] + [1, 2, 3, 4] ``` While these are not (`false`): ``` [4, 1, 2, 5] [8, 7, 6, 5, 4, 3] [6, 7, 4, 8] [7, 6, 5, 4, 3, 2, 1] ```
def circularly_sorted(arr): temp = sorted(arr) for i in range(len(arr)): temp = temp[1:]+[temp[0]] if temp==arr : return 1 return 0