#include <algorithm> #include <list> using std::list; // just pretend that std::list::merge doesn't exist when you're reading this code // this isn't that great a solution even if you do that, but I wanted to mirror the forked kumite a little list<int> merge_list(list<int> a, list<int> b) { auto i = a.begin(); for (auto x: b) { i = a.insert(find_if(i, a.end(), [x](int &y) {return y > x;}), x); } return a; }
- #include <algorithm>
- #include <list>
using std::list; // I actually never use `using`, just wanted to show that these are just lists- using std::list;
- // just pretend that std::list::merge doesn't exist when you're reading this code
- // this isn't that great a solution even if you do that, but I wanted to mirror the forked kumite a little
- list<int> merge_list(list<int> a, list<int> b) {
auto itr = b.begin();for (auto i = a.begin(); i != a.end() && itr != b.end(); ++i) {if (*itr < *i) {i = a.insert(i, *itr++);}}for (; itr != b.end(); ++itr) {a.push_back(*itr);- auto i = a.begin();
- for (auto x: b) {
- i = a.insert(find_if(i, a.end(), [x](int &y) {return y > x;}), x);
- }
- return a;
- }
Your boss give you a task to format some integer numbers like this:
123456789 -> 123,456,789
So, you should write a function f
which recieves a integer number and returns a string which every 3 consecutive digits are splitted with ,
symbol from left to right.
def f(n, G=3): s = str(n) r = len(s) % G return ','.join([x for x in [s[:r]] + [s[i:i+G] for i in range(r, len(s), G)] if x])
def f(n):G = 3- def f(n, G=3):
- s = str(n)
- r = len(s) % G
def make(s):if r:yield s[:r]for i in range(r, len(s), G):yield s[i:i+G]return ','.join(make(s))- return ','.join([x for x in [s[:r]] + [s[i:i+G] for i in range(r, len(s), G)] if x])
Your boss give you a task to format some integer numbers like this:
123456789
-> 123,456,789
So, you should write a function f
which recieves a integer number and returns a string which every 3 consecutive digits are splitted with ,
symbol from left to right.
def f(n):
G = 3
s = str(n)
r = len(s) % G
def make(s):
if r:
yield s[:r]
for i in range(r, len(s), G):
yield s[i:i+G]
return ','.join(make(s))
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
test.assert_equals(f(100), '100')
test.assert_equals(f(1000), '1,000')
test.assert_equals(f(12345), '12,345')
test.assert_equals(f(123456789), '123,456,789')
test.assert_equals(f(10000000000), '10,000,000,000')
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
You are asking to write a function, which takes N lists as input, such so len(l1) >= len(l2) >= .. len(lN) and interleave them into a single list. If you get a single list in input, then just return original list as is.
Example:
You have ['a1','a1','a3'], ['b1','b2'],['c1'] to interleave. Finually, you should get ['a1','b1','c1','a2','b2','c1'].
def interleave(*argv):
lists = len(argv)
if 1 == lists:
return argv[0]
else:
o = [x for t in zip(*argv) for x in t]
for i in reversed(range(0, lists - 1)):
l = len(argv[i + 1])
lists = []
for _list in argv[:i+1]:
lists.append(_list[l:])
o += [x for t in zip(*lists) for x in t]
return o
test.assert_equals(interleave([]), [])
test.assert_equals(interleave([1,2]), [1,2])
test.assert_equals(interleave([1,2,3], ['a','b','c']), [1,'a',2,'b',3,'c'])
test.assert_equals(interleave(list(range(1, 11, 3)), list(range(2, 11, 3)), list(range(3, 11, 3))), list(range(1, 11)))