Persistent Command Line Display - ruby

I am writing a little Sudoku solving app in Ruby that just outputs to the terminal. For example:
ruboku $ ruby grid.rb
+---+---+---+---+---+---+---+---+---+
| 2 | 8 | 5 | | 3 | 1 | 7 | 6 | 9 |
+---+---+---+---+---+---+---+---+---+
| 5 | | 4 | 8 | 7 | 2 | 1 | 9 | 3 |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
Is there a way to keep this as a persistent display such that when there is new information to show it doesn't print a new line, but updates the grid that is already displaying?
I've seen some tools such as ngrok who do this.
Thanks for your time,
Tom

Depending on your needs, erasing the screen and creating the grid again could be good enough :
def show_grid
line = '+---+---+---+---+---+---+---+---+---+'
puts line
9.times do
row = (1..9).map { rand(9) + 1 }
puts '| ' + row.join(' | ') + ' |'
puts line
end
end
def clear_screen
system('clear') || system('cls')
end
loop do
clear_screen
show_grid
sleep 1
end
It outputs
+---+---+---+---+---+---+---+---+---+
| 1 | 2 | 7 | 3 | 4 | 9 | 8 | 6 | 5 |
+---+---+---+---+---+---+---+---+---+
| 1 | 8 | 1 | 7 | 6 | 2 | 2 | 1 | 3 |
+---+---+---+---+---+---+---+---+---+
| 2 | 2 | 1 | 2 | 2 | 8 | 2 | 3 | 6 |
+---+---+---+---+---+---+---+---+---+
| 5 | 3 | 6 | 1 | 5 | 3 | 2 | 7 | 9 |
+---+---+---+---+---+---+---+---+---+
| 9 | 7 | 7 | 4 | 7 | 2 | 2 | 9 | 1 |
+---+---+---+---+---+---+---+---+---+
| 5 | 9 | 1 | 9 | 3 | 7 | 8 | 3 | 1 |
+---+---+---+---+---+---+---+---+---+
| 8 | 5 | 4 | 7 | 3 | 2 | 2 | 5 | 3 |
+---+---+---+---+---+---+---+---+---+
| 4 | 7 | 1 | 1 | 4 | 8 | 4 | 1 | 1 |
+---+---+---+---+---+---+---+---+---+
| 1 | 1 | 8 | 4 | 2 | 4 | 8 | 3 | 8 |
+---+---+---+---+---+---+---+---+---+
then
+---+---+---+---+---+---+---+---+---+
| 6 | 4 | 4 | 3 | 5 | 5 | 8 | 9 | 1 |
+---+---+---+---+---+---+---+---+---+
| 7 | 7 | 7 | 3 | 3 | 2 | 8 | 7 | 6 |
+---+---+---+---+---+---+---+---+---+
| 6 | 2 | 2 | 1 | 4 | 7 | 1 | 1 | 9 |
+---+---+---+---+---+---+---+---+---+
| 3 | 8 | 7 | 7 | 7 | 9 | 7 | 4 | 4 |
+---+---+---+---+---+---+---+---+---+
| 4 | 1 | 2 | 8 | 6 | 7 | 1 | 9 | 3 |
+---+---+---+---+---+---+---+---+---+
| 6 | 7 | 7 | 5 | 1 | 7 | 6 | 7 | 4 |
+---+---+---+---+---+---+---+---+---+
| 8 | 2 | 3 | 5 | 3 | 5 | 4 | 7 | 2 |
+---+---+---+---+---+---+---+---+---+
| 5 | 9 | 3 | 2 | 4 | 2 | 9 | 6 | 3 |
+---+---+---+---+---+---+---+---+---+
| 1 | 7 | 8 | 9 | 4 | 3 | 4 | 1 | 5 |
+---+---+---+---+---+---+---+---+---+
then...
It should work on Windows/Linux/MacOs.
For anything more complex, you'll need an ncurses gem.

So the question is marked as answered I thought I would write up the advice given to me in the comments.
It looks like the ncurses ruby gem and vedeu offer the functionality that I need. Another alternative would be to clear the scren and reprint the output.

Related

Combinate of values in a table to get the sum of each combination

I have a table with numeric data that i need make diferent combinations itself.
For example:
| A |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
I need to combine this single column to get the next result:
| A | B | C | D |
| - | - | - | - |
| 1 | | | |
| 1 | 2 | | |
| 1 | 2 | 3 | |
| 1 | 2 | 3 | 4 |
| 1 | 2 | | 4 |
| 1 | | 3 | |
| 1 | | 3 | 4 |
| 1 | | | 4 |
| | 2 | | |
| | 2 | 3 | |
| | 2 | 3 | 4 |
| | 2 | | 4 |
| | | 3 | |
| | | 3 | 4 |
| | | | 4 |
At the end of the table, i have to create a column with the Count of every column that has data and another column that contains the sums of number of each columns.
Maybe it sound very difficult or impossible, but I haven't a way to make it work.
I have try to "Cross Join" from SQL but didn't got the expected result.
Help!
In this case, you can solve this by counting in binary ending with the digits being the number of numbers in the set. etc. the starting set 2568 would end with 1111. this binary number would decide if you show that number in each row. Heres a table of how it would work.
| A |
|---|
| 2 |
| 5 |
| 6 |
| 8 |
A
B
C
D
Binary
Row number
8
0001
1
6
0010
2
6
8
0011
3
5
0100
4
5
8
0101
5
5
6
0110
6
5
6
8
0111
7
2
1000
8
2
8
1001
9
2
6
1010
10
2
6
8
1011
11
2
5
1100
12
2
5
8
1101
13
2
5
6
1110
14
2
5
6
8
1111
15

Pivot Table in Hive and Create Multiple Columns for Unique Combinations

I want to pivot the following table
| ID | Code | date | qty |
| 1 | A | 1/1/19 | 11 |
| 1 | A | 2/1/19 | 12 |
| 2 | B | 1/1/19 | 13 |
| 2 | B | 2/1/19 | 14 |
| 3 | C | 1/1/19 | 15 |
| 3 | C | 3/1/19 | 16 |
into
| ID | Code | mth_1(1/1/19) | mth_2(2/1/19) | mth_3(3/1/19) |
| 1 | A | 11 | 12 | 0 |
| 2 | B | 13 | 14 | 0 |
| 3 | C | 15 | 0 | 16 |
I am new to hive, i am not sure how to implement it.
NOTE: I don't want to do mapping because my month values change over time.

Ruby Net-SSH get on-login text and send data

when I run this command from my linux terminal:
ssh -p 12643 sudoku#ringzer0team.com
and enter the password, dg43zz6R0E, I get this message:
Linux ld64webdmz 3.2.0-4-amd64 #1 SMP Debian 3.2.82-1 x86_64
Last login: Fri Sep 22 09:19:02 2017 from 39.188.121.75
RingZer0 Team Online CTF
The sudoku challenge
+---+---+---+---+---+---+---+---+---+
| | 4 | 8 | | | 1 | 3 | 5 | 2 |
+---+---+---+---+---+---+---+---+---+
| 6 | 7 | | | 5 | | | 4 | |
+---+---+---+---+---+---+---+---+---+
| | | | | 4 | 8 | 6 | 7 | |
+---+---+---+---+---+---+---+---+---+
| 4 | | | | 1 | 3 | 5 | 2 | 9 |
+---+---+---+---+---+---+---+---+---+
| | | 3 | 5 | 2 | | | | 6 |
+---+---+---+---+---+---+---+---+---+
| | | 9 | | | 6 | | 1 | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | 4 |
+---+---+---+---+---+---+---+---+---+
| | | | 2 | | | | | |
+---+---+---+---+---+---+---+---+---+
| 2 | | | | | | 1 | | |
+---+---+---+---+---+---+---+---+---+
Solve this sudoku in less than 10 seconds and you'll get the flag.
Submit all the sudoku table using this format from left to right 1,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,1...
Solution:
Using Net-SSH from Ruby, how can I get that on-login message and send a response?
This is what I have:
#!/usr/bin/ruby
require'net/ssh';
Net::SSH.start('ringzer0team.com', 'sudoku', :password => 'dg43zz6R0E', :port => 12643) do|ssh|
# read that on_login text, solve and send output
p ssh.exec!(((1..9).to_a*9).join(',')+"\n"); # trying to send data
end
It does not terminate (does not get past the call to exec!).
I'm just asking about how to interact with the session (get and send data), not on how to solve the sudoku.

How to sort data by hierarchy

Lets say I have some data like this
Name | ID | ParentID | Level
------------+-----+----------+-------
Fruits | 1 | 0 | 1
Vegetables | 2 | 0 | 1
Apple | 3 | 1 | 2
Banana!! | 4 | 1 | 2
Tomato | 5 | 2 | 2
Potato | 6 | 2 | 2
red | 7 | 5 | 3
green | 8 | 5 | 3
How to sort (compare) this data to get a result like this:
Name | ID | ParentID | Level
------------+-----+----------+---------
Fruits | 1 | 0 | 1
Apple | 3 | 1 | 2
Banana!! | 4 | 1 | 2
Vegetables | 2 | 0 | 1
Tomato | 5 | 2 | 2
red | 7 | 5 | 3
green | 8 | 5 | 3
Potato | 6 | 2 | 2
Background is that I have a model-collection with models and I want to add them according to the hierarchy given by ID/ParentID

Sort values in an associative array pl/sql

If ID is even I must sort the values that correspond to that ID DESC , if the ID is odd I must sort the values ASC. This is the table called Grades.
ID|COL1|COL2|COL3|COL4|COL5|COL6|COL7|
1 | 6 | 3 | 8 | 4 | 7 | 8 | 4 |
2 | 5 | 7 | 9 | 2 | 1 | 7 | 8 |
3 | 2 | 7 | 4 | 8 | 1 | 5 | 9 |
4 | 8 | 4 | 7 | 9 | 4 | 1 | 4 |
5 | 7 | 5 | 2 | 5 | 2 | 6 | 4 |
The result must be this:
ID|COL1|COL2|COL3|COL4|COL5|COL6|COL7|
1 | 3 | 4 | 4 | 6 | 7 | 8 | 8 |
2 | 9 | 8 | 7 | 7 | 5 | 2 | 1 |
3 | 1 | 2 | 4 | 5 | 7 | 8 | 9 |
4 | 9 | 8 | 7 | 4 | 4 | 4 | 1 |
5 | 2 | 2 | 4 | 5 | 5 | 6 | 7 |
As you can see ID=1->odd number so the values must be sorted ASC
This is the code so far:
declare
type grades_array is table of grades%rowtype index by pls_integer;
grades_a grades_array;
cnt number;
begin
Select count(id) into cnt from grades;
For i in 1..cnt loop
--I used an associative array
Select * into grades_a(i) from grades where grades.id=i;
end loop;
For i in grades_a.FIRST..grades_a.LAST loop
if (mod(grades_a(i).id,2)=1)then .......
--I don't know how to sort the specific rows, in this case ASC
--dbms_output.put_line(grades_a(i).col1);
end if;
end loop;
--Also it is specified in the exercise that the table can change, e.g add more columns
end;
I would simply use PIVOT/UNPIVOT for this.
First UNPIVOT the table and assign a rank to each column value in ascending/descending order.
SQL Fiddle
Query 1:
SELECT id,
colval,
ROW_NUMBER () OVER (
PARTITION BY id
ORDER BY CASE MOD (id, 2) WHEN 1 THEN colval END,
CASE MOD (id, 2) WHEN 0 THEN colval END DESC) r
FROM x UNPIVOT (colval FOR colname
IN (col1 AS 'col1', col2 AS 'col2', col3 AS 'col3', col4 AS 'col4',
col5 AS 'col5', col6 AS 'col6', col7 AS 'col7')
)
Results:
| ID | COLVAL | R |
|----|--------|---|
| 1 | 3 | 1 |
| 1 | 4 | 2 |
| 1 | 4 | 3 |
| 1 | 6 | 4 |
| 1 | 7 | 5 |
| 1 | 8 | 6 |
| 1 | 8 | 7 |
| 2 | 9 | 1 |
| 2 | 8 | 2 |
| 2 | 7 | 3 |
| 2 | 7 | 4 |
| 2 | 5 | 5 |
| 2 | 2 | 6 |
| 2 | 1 | 7 |
| 3 | 1 | 1 |
| 3 | 2 | 2 |
| 3 | 4 | 3 |
| 3 | 5 | 4 |
| 3 | 7 | 5 |
| 3 | 8 | 6 |
| 3 | 9 | 7 |
| 4 | 9 | 1 |
| 4 | 8 | 2 |
| 4 | 7 | 3 |
| 4 | 4 | 4 |
| 4 | 4 | 5 |
| 4 | 4 | 6 |
| 4 | 1 | 7 |
| 5 | 2 | 1 |
| 5 | 2 | 2 |
| 5 | 4 | 3 |
| 5 | 5 | 4 |
| 5 | 5 | 5 |
| 5 | 6 | 6 |
| 5 | 7 | 7 |
Then PIVOT the result based on the rank.
Query 2:
WITH pivoted AS (
SELECT id,
colval,
ROW_NUMBER () OVER (
PARTITION BY id
ORDER BY CASE MOD (id, 2) WHEN 1 THEN colval END,
CASE MOD (id, 2) WHEN 0 THEN colval END DESC) r
FROM x UNPIVOT (colval FOR colname
IN (col1 AS 'col1', col2 AS 'col2', col3 AS 'col3', col4 AS 'col4',
col5 AS 'col5', col6 AS 'col6', col7 AS 'col7')
)
)
SELECT * FROM pivoted
PIVOT (MAX (colval)
FOR r
IN (1 AS col1, 2 AS col2, 3 AS col3, 4 AS col4,
5 AS col5, 6 AS col6, 7 AS col7))
Results:
| ID | COL1 | COL2 | COL3 | COL4 | COL5 | COL6 | COL7 |
|----|------|------|------|------|------|------|------|
| 1 | 3 | 4 | 4 | 6 | 7 | 8 | 8 |
| 2 | 9 | 8 | 7 | 7 | 5 | 2 | 1 |
| 3 | 1 | 2 | 4 | 5 | 7 | 8 | 9 |
| 4 | 9 | 8 | 7 | 4 | 4 | 4 | 1 |
| 5 | 2 | 2 | 4 | 5 | 5 | 6 | 7 |

Resources