Skip to content

Commit c52c44d

Browse files
Initial commit for Michal CTO exercises
Initial commit for exercises assigned to me by Michal. Among others: * cat - unix command implementation in ruby * grep - unix command implementation in ruby * ls - unix command implementation in ruby * ps - unix command implementation in ruby * pwd - unix command implementation in ruby * wc - unix command implementation in ruby * ONP converter (reverse polish notation) * Roman numerals to decimals converter
1 parent 6f4a6ee commit c52c44d

File tree

12 files changed

+416
-0
lines changed

12 files changed

+416
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

michal_zadania/cat/bin/cat.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env ruby
2+
3+
unless filename = ARGV.first
4+
puts "You need to pass name of the file"
5+
exit 0
6+
end
7+
8+
9+
File::open filename do |file|
10+
print file.read
11+
end
12+

michal_zadania/grep/bin/grep.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env ruby
2+
3+
needle, filename = ARGV
4+
5+
unless needle && filename
6+
puts 'You need to pass needle and filename argument'
7+
exit 0
8+
end
9+
10+
def colorize text, color_code
11+
"\e[#{color_code}m#{text}\e[0m"
12+
end
13+
14+
File::open filename do |file|
15+
while line = file.gets
16+
print colorize line, 31 if line.include? needle
17+
end
18+
end
19+

michal_zadania/grep/kaka

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kaka kakak

michal_zadania/ls/bin/ls.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env ruby
2+
3+
# This works for bare 'ls' and partially for 'ls -l'
4+
5+
6+
# please ignore this from here --------------------
7+
8+
#puts sprintf("%o",File.world_readable?('kaka'))
9+
# puts "\n\n\n\n"
10+
#
11+
=begin
12+
s = File.stat('kaka')
13+
puts s.inspect
14+
puts "\n\n\n\n"
15+
s.mode
16+
puts sprintf("%o", s.mode)
17+
puts s.uid
18+
puts s.gid
19+
=end
20+
# till here ( I will probably need some of that)------
21+
22+
arguments = ARGV
23+
24+
if arguments.length > 1
25+
puts 'You can\'t pass more than one argument'
26+
exit 0
27+
end
28+
29+
argument = arguments.first
30+
31+
if argument.nil?
32+
Dir.glob('*').sort.each {|file| print "#{file} "}
33+
34+
print "\n"
35+
exit 0
36+
end
37+
38+
39+
# identify passed options if there is an argument at all
40+
if argument =~ /-[a-zA-Z0-9]+/
41+
argument_chars = argument.chars
42+
43+
# remove '-' from arguments' characters
44+
argument_chars.shift
45+
46+
if argument_chars.count('l') > 0
47+
Dir::glob('*').sort.each do |file|
48+
if File::file? file
49+
print '-'
50+
else
51+
print 'd'
52+
end
53+
54+
permissions = File::world_readable? file
55+
56+
if permissions.nil?
57+
print '-' * 9
58+
else
59+
permissions = sprintf "%o", permissions
60+
permissions = permissions.to_s.chars
61+
permissions[0]
62+
end
63+
64+
puts "rw-rw-r-- 1 visuality visuality 777 maj 11 15:44 #{file}"
65+
end
66+
end
67+
68+
else
69+
puts 'You need to pass argument in the form of hyphen followed by parameters as in "-la"'
70+
exit 0
71+
end

michal_zadania/onp/bin/onp.rb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env ruby
2+
3+
# todo write it as a class/classes
4+
5+
OPERATOR_REGEX = /[+\-*\/^]/
6+
NUMBER_REGEX = /[0-9]/
7+
OPERATOR_PRIORITIES = { '^' => 4,
8+
'*' => 3, '/' => 3,
9+
'+' => 2, '-' => 2,
10+
'=' => 1,
11+
'(' => 0, ')' => 0 }
12+
13+
def extract_number index, symbols
14+
number_string = ''
15+
16+
while symbols[index] =~ /[0-9]/
17+
number_string += symbols[index]
18+
index += 1
19+
end
20+
21+
return number_string
22+
end
23+
24+
def convert_to_onp chars
25+
output = []
26+
stack = []
27+
error = nil
28+
29+
index = 0
30+
31+
breakpoint = chars.length
32+
33+
loop do
34+
case
35+
when chars[index] == '('
36+
stack.push chars[index]
37+
index += 1
38+
when chars[index] == ')'
39+
until stack.last == '('
40+
output.push stack.pop
41+
end
42+
stack.pop
43+
index += 1
44+
when chars[index] =~ NUMBER_REGEX
45+
number = extract_number index, chars
46+
index += number.length
47+
output.push number
48+
when chars[index] =~ OPERATOR_REGEX
49+
while stack.last && OPERATOR_PRIORITIES[stack.last] >= OPERATOR_PRIORITIES[chars[index]]
50+
output.push stack.pop
51+
end
52+
stack.push chars[index]
53+
index += 1
54+
when chars[index] =~ /\s/
55+
index += 1
56+
else
57+
error = "Program encountered unrecognized character: '#{chars[index]}'"
58+
break
59+
end
60+
break if index >= breakpoint
61+
end
62+
63+
until stack.empty?
64+
output.push stack.pop
65+
end
66+
67+
return error ? error : output
68+
end
69+
70+
filename = ARGV.first
71+
72+
unless filename
73+
puts "You need to pass filename argument to the script"
74+
exit 0
75+
end
76+
77+
File::open filename do |file|
78+
79+
file.readlines.each do |line|
80+
line = line.chomp
81+
82+
onp = convert_to_onp line.chars
83+
84+
if !onp.is_a? String
85+
print line, " becomes "
86+
onp.each {|element| print element + ' '}
87+
print "\n"
88+
else
89+
puts onp
90+
end
91+
end
92+
end
93+

michal_zadania/onp/test.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1+33*(4+2*3)
2+
3 / (52 + y * x)
3+
335*(9-2*44+167)-22
4+
6+3*82*(7-4)
5+
(2+6)*(9-8+445)-3
6+
((2+5)-(3-12))*3+4

michal_zadania/ps/bin/ps.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env ruby
2+
3+
# Bare 'ps' is much harder than 'ps -aux'.
4+
# Yet - I started with bare ps and plan on soon delivering it
5+
# along with 'ps -aux'
6+
7+
Dir.chdir '/proc'
8+
9+
all_processes = Dir.glob('*').select {|file| file =~ /[0-9]+/ }
10+
11+
all_processes.each do |process_id|
12+
file = open("/proc/#{process_id}/status")
13+
end
14+
15+
all_processes = all_processes.map {|process_id| process_id.to_i}
16+
puts "PID"
17+
puts all_processes.sort
18+
19+
puts Process.pid
20+
puts "\n\n\n"
21+
22+
puts ENV['USER']
23+
puts Process.euid

michal_zadania/pwd/bin/pwd.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env ruby
2+
3+
puts Dir::pwd
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env ruby
2+
3+
ROMAN_TO_DEC = { 'I' => 1,
4+
'IV' => 4,
5+
'V' => 5,
6+
'IX' => 9,
7+
'X' => 10,
8+
'XL' => 40,
9+
'L' => 50,
10+
'XC' => 90,
11+
'C' => 100,
12+
'CD' => 400,
13+
'D' => 500,
14+
'CM' => 900,
15+
'M' => 1000 }
16+
17+
DOUBLE_ROMAN = ['IV', 'IX', 'XL', 'XC', 'CD', 'CM']
18+
19+
def validate_roman? line
20+
line.chars.each do |char|
21+
return false unless ROMAN_TO_DEC.key? char
22+
end
23+
24+
return true
25+
end
26+
27+
def roman_to_dec line
28+
sum = 0
29+
chars = line.chars
30+
31+
i = 0
32+
i_end = chars.length
33+
34+
loop do
35+
if i+1 < i_end && DOUBLE_ROMAN.include?(chars[i] + chars[i+1])
36+
sum += ROMAN_TO_DEC[chars[i] + chars[i+1]]
37+
i += 1
38+
elsif i+1 <= i_end
39+
sum += ROMAN_TO_DEC[chars[i]]
40+
else
41+
end
42+
43+
i += 1
44+
break if i >= i_end
45+
end
46+
47+
return sum > 0 ? sum : 'empty line'
48+
end
49+
50+
####################################### body
51+
52+
filename = ARGV.first
53+
54+
File::open filename do |file|
55+
while line = file.gets
56+
line = line.chomp
57+
58+
if validate_roman? line
59+
puts roman_to_dec line
60+
else
61+
puts "Not a roman numeral"
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)