aboutsummaryrefslogtreecommitdiff
path: root/challenge-224/barroff/awk/ch-1.awk
blob: 04a2ebcd4c5203a1dff101b6976bee16c5d7aa91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env awk

function special_notes(source, target) {
    split(source, source_split, //);
    split(target, target_split, //);
    for (i in source_split) {
        source_dict[source_split[i]]++;
    }
    for (i in target_split) {
        target_dict[target_split[i]]++;
    }
    for (i in target_dict) {
        if (source_dict[i] < target_dict[i]) {
            delete source_split;
            delete source_dict;
            delete target_split;
            delete target_dict;
            return 0;
        }
    }
    return 1;
}

function works_for(source, target, success) {
    if (success) {
        print "Works for source = " source ", and target = " target;
    } else {
        print "Does not work for source = " source ", and target = " target;
    }
}

END {
    works_for("abc", "xyz", !special_notes("abc", "xyz"));
    works_for("scriptinglanguage", "perl", special_notes("scriptinglanguage", "perl"));
    works_for("aabbcc", "abc", special_notes("aabbcc", "abc"));
}