Skip to content

Commit 899e393

Browse files
committed
rename the directory & add shell problems
1 parent 766f6c6 commit 899e393

File tree

186 files changed

+349
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+349
-182
lines changed

README.md

+196-182
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

shell/TenthLine.sh

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Source : https://leetcode.com/problems/tenth-line/
2+
# Author : Hao Chen
3+
# Date : 2015-03-31
4+
5+
##################################################################################
6+
#
7+
# How would you print just the 10th line of a file?
8+
#
9+
# For example, assume that file.txt has the following content:
10+
#
11+
# Line 1
12+
# Line 2
13+
# Line 3
14+
# Line 4
15+
# Line 5
16+
# Line 6
17+
# Line 7
18+
# Line 8
19+
# Line 9
20+
# Line 10
21+
#
22+
# Your script should output the tenth line, which is:
23+
#
24+
# Line 10
25+
#
26+
# [show hint]
27+
# Hint:
28+
# 1. If the file contains less than 10 lines, what should you output?
29+
# 2. There's at least three different solutions. Try to explore all possibilities.
30+
##################################################################################
31+
32+
#!/bin/sh
33+
34+
35+
# Read from the file file.txt and output the tenth line to stdout.
36+
37+
# Solution One
38+
awk 'NR==10{print $0}' file.txt
39+
40+
# Solution Two
41+
sed -n '10p' file.txt
42+

shell/TransposeFile.sh

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Source : https://leetcode.com/problems/transpose-file/
2+
# Author : Hao Chen
3+
# Date : 2015-03-31
4+
5+
##################################################################################
6+
#
7+
# Given a text file file.txt, transpose its content.
8+
#
9+
# You may assume that each row has the same number of columns and each field is separated by the ' ' character.
10+
#
11+
# For example, if file.txt has the following content:
12+
#
13+
# name age
14+
# alice 21
15+
# ryan 30
16+
#
17+
# Output the following:
18+
#
19+
# name alice ryan
20+
# age 21 30
21+
##################################################################################
22+
23+
#!/bin/sh
24+
25+
# Read from the file file.txt and print its transposed content to stdout.
26+
awk '
27+
{
28+
for (i = 1; i <= NF; i++) {
29+
if (NR == 1){
30+
s[i]=$i;
31+
}else{
32+
s[i] = s[i] " " $i;
33+
}
34+
}
35+
}
36+
END {
37+
for (i = 1; s[i] != ""; i++) {
38+
print s[i];
39+
}
40+
}' file.txt
41+
42+
43+

shell/ValidPhoneNumbers.sh

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Source : https://leetcode.com/problems/valid-phone-numbers/
2+
# Author : Hao Chen
3+
# Date : 2015-03-31
4+
5+
##################################################################################
6+
#
7+
# Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.
8+
#
9+
# You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
10+
#
11+
# You may also assume each line in the text file must not contain leading or trailing white spaces.
12+
#
13+
# For example, assume that file.txt has the following content:
14+
#
15+
# 987-123-4567
16+
# 123 456 7890
17+
# (123) 456-7890
18+
#
19+
# Your script should output the following valid phone numbers:
20+
#
21+
# 987-123-4567
22+
# (123) 456-7890
23+
##################################################################################
24+
25+
#!/bin/sh
26+
27+
28+
# Read from the file file.txt and output all valid phone numbers to stdout.
29+
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt
30+

shell/WordFrequency.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Source : https://leetcode.com/problems/word-frequency/
2+
# Author : Hao Chen
3+
# Date : 2015-03-31
4+
5+
##################################################################################
6+
#
7+
# Write a bash script to calculate the frequency of each word in a text file words.txt.
8+
#
9+
# For simplicity sake, you may assume:
10+
#
11+
# words.txt contains only lowercase characters and space ' ' characters.
12+
# Each word must consist of lowercase characters only.
13+
# Words are separated by one or more whitespace characters.
14+
#
15+
# For example, assume that words.txt has the following content:
16+
# the day is sunny the the
17+
# the sunny is is
18+
#
19+
# Your script should output the following, sorted by descending frequency:
20+
#
21+
# the 4
22+
# is 3
23+
# sunny 2
24+
# day 1
25+
#
26+
# Note:
27+
# Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.
28+
#
29+
# [show hint]
30+
# Hint:
31+
# Could you write it in one-line using Unix pipes?
32+
##################################################################################
33+
34+
#!/bin/sh
35+
36+
# Read from the file words.txt and output the word frequency list to stdout.
37+
cat words.txt | tr [:space:] "\n" | sed '/^$/d' | tr '[:upper:]' '[:lower:]'|sort|uniq -c|sort -nr | awk '{ print $2,$1}'
38+

0 commit comments

Comments
 (0)