-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-readme.pl
105 lines (86 loc) · 2.8 KB
/
gen-readme.pl
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use JSON;
my %dtag = (
easy => '<span style="color: #00af9b;">Easy</span>',
medium => '<span style="color: #ffb800;">Medium</span>',
hard => '<span style="color: #ff2d55;">Hard</span>',
);
my %dcount = (
easy => 0,
medium => 0,
hard => 0,
);
my %dtotal = (
easy => 0,
medium => 0,
hard => 0,
);
my %problems;
foreach my $problem (@{from_json(`bash ./fetch-problem-list.bash`)}) {
$problems{$problem->{no}} = $problem;
$dtotal{$problem->{difficulty}}++;
}
my @fileinfo;
foreach my $difficulty ('easy', 'medium', 'hard') {
foreach my $filepath (<code/$difficulty/*>) {
my $filename = basename($filepath);
my ($no) = $filename =~ /^(\d+)/;
my ($ext) = $filename =~ /\.(\w+)$/;
my ($title, $link, $tc, $sc);
open my $fd, '<', $filepath or die "Can't open $filepath: $!";
while (<$fd>) {
$title = $1 if m"^(?://|#|--) Title: (.*)$";
$link = $1 if m"^(?://|#|--) Link: (.*)$";
$tc = $1 if m"^(?://|#|--) Time complexity: (.*)$";
$sc = $1 if m"^(?://|#|--) Space complexity: (.*)$";
}
close $fd;
my $problem = $problems{$no};
warn "Difficulty mismatched! ($filename)" if $difficulty ne $problem->{difficulty};
warn "Title mismatched! ($filename)" if $title ne $problem->{title};
warn "Link mismatched! ($filename)" if $link ne "https://leetcode.com/problems/$problem->{slug}/";
push @fileinfo, {
difficulty => $dtag{$difficulty},
no => $no // 9999,
title => $title || undef,
link => $link || undef,
filename => $filename,
filepath => $filepath,
ext => $ext,
tc => $tc // 'N/A',
sc => $sc // 'N/A',
};
$dcount{$difficulty}++;
}
}
print <<EOF;
# leetcode
[![Generate README.md](https://github.com/zetaraku/leetcode/actions/workflows/generate-readme.yaml/badge.svg)](https://github.com/zetaraku/leetcode/actions/workflows/generate-readme.yaml)
My [LeetCode](https://leetcode.com/) submissions
- $dtag{easy}: $dcount{easy} / $dtotal{easy}
- $dtag{medium}: $dcount{medium} / $dtotal{medium}
- $dtag{hard}: $dcount{hard} / $dtotal{hard}
## Index
EOF
my @languages = (
{ name => 'C++', ext => 'cpp' },
{ name => 'SQL', ext => 'sql' },
{ name => 'TypeScript', ext => 'ts' },
);
foreach my $language (@languages) {
print <<EOF;
### $language->{name}
| No. | Difficulty | Title | Code | Time complexity | Space complexity |
| ---:| ---------- | ----- | ---- | --------------- | ---------------- |
EOF
foreach (sort { $a->{no} <=> $b->{no} } grep { $_->{ext} eq $language->{ext} } @fileinfo) {
my $title = defined $_->{link} ? "[$_->{title}]($_->{link})" : "$_->{title}";
my $code = "[$_->{filename}](./$_->{filepath})";
print <<LINE;
| $_->{no} | $_->{difficulty} | $title | $code | `$_->{tc}` | `$_->{sc}` |
LINE
}
}