Ad
Code
Diff
  • #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;
    • }
Strings
Data Types

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.

Code
Diff
  • f = lambda n: '{:,}'.format(n)
    • 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))
    • f = lambda n: '{:,}'.format(n)
Strings
Data Types
Code
Diff
  • 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])
Strings
Data Types

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))
Fundamentals

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