Posted on

I did it, but not for myself, just for fun.

Excel Columns

The row numbers in MS EXCEL are represented by combinations of the English alphabet, your task is to convert the numbers to the MS row number representation. If there’s a “…., you should leave it there, it omits some of the row numbers.

For example,

Given “1 2 3 …. 26 27 28 29 …. 52 53 54 55 ….”

Convert it to “a b c …. z aa ab ac ad …. “

Submission

s = str(input())
#s = "1 2 3 .... 26 27 28 29 .... 52 53 54 55 ...."

l = s.split()
# y: 25 z: 26
d1 = 'zabcdefghijklmnopqrstuvwxyz'

res = list()
for x in l:
    if x == '....':
        res.append(x)
        continue
    t = int(x) # a 1
    s = ''
    
    while t > 0:
        n = t % 26
        if n == 0:
            s = 'z' + s
            t = t // 26 - 1
        else:
            s = d1[n] + s
            t = t // 26

    res.append(s)
print(" ".join(res))

Sort the Fruit

Input: First line is the number of farmers n. Then m lines after, each line containg the following information

fruit worker_id weight

An input may like the following

5
Apple 1 65
Orange 5 45
Apple 2 56
Apple 3 44
Orange 4 34
Orange 5 67

Sort the rows first by the name of the fruit, depending on the order of first occurrence, then in each of the fruit, sort by their weight, in ascending order.

#include <iostream>
#include <map>
#include <string>
#include <set>

using namespace std;

int main() {
    int n; cin >> n;
    map<string, set<pair<int, int>>> Map; // load, worker_id
    int count = 0;
    map<int, string> fruit_order;
    set<string> fruit_exist;
    string fruit;
    int worker, load;

    while(cin >> fruit >> worker >> load) {
        if(fruit_exist.find(fruit) == fruit_exist.end()) {
            fruit_exist.insert(fruit);
            fruit_order[count++] = fruit;
        }
        Map[fruit].insert({load, worker});
    }

    for(auto iter = fruit_order.begin(); iter != fruit_order.end(); ++iter) {
        string f = iter->second; // fruit
        cout << f << endl;
        for (auto iter2 = Map[f].begin(); iter2 != Map[f].end(); ++iter2) {
            cout << f << " " << iter2->second << " " << iter2->first << endl;
        }
    }

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *