Find the smallest sub square matrix that contains all the vowels - algorithm

You are provided with 2-D Matrix, A, such that
1. A (MxN), is 2-Dimensional Matrix
2. A[i][j] E [A-Z]
you have to find the smallest sub square matrix, such that
it contains all the vowels i.e. {A E, I, O, U}.
Input: A[][] ->
A E I O B 1 2 3 4 5
C D U Z O 6 7 8 9 10
P O A E K 11 12 13 14 15
B R E A K 16 17 18 19 20
Possible Outputs:
A E I 1 2 3
C D U 5 6 7
P O A 9 10 11
I O B 3 4 5
U Z O 8 9 10
A E K 13 14 15
so, size here will be 3x3 square matrix, so it is 3.
Please help me to find out the logic behind this problem.

Related

How to build special turing machine

Good day. I have a question. Many people are familiar with the Turing machine. The following task arose, which I can’t solve for a long time: there is an alphabet consisting of the letters "X", "Y" "Z", if the number of letters "Z" in the word is exactly 2 more than the letters "X", replace the second letter "Z" with "X". Otherwise, leave the word unchanged. Considering that I cannot change the original word and the tape is infinite (that is, I cannot write an infinite number of states for the machine), I do not understand how to do this.
Just to make it more clear, if the input is for example:
XXXYZZZZZ
then the output you need is:
XXXYZXZZZZ
And if the input is:
XXYZZ
Then everything needs to stay the same, since (number of Z's) - (number of X's) != 2
XXYZZ
If I understood the problem correctly, in the way I defined above, then here comes the solution with Morphett's TM Simulator, with $ sign as left-end marker:
1 $ $ r 1
1 X A r 2
1 Y Y r 4
2 X X r 2
2 B B r 2
2 Y Y r 2
2 Z B R 3
3 Y Y l 3
3 B B l 3
3 X X l 3
3 A A r 1
3 Z Z l 3
4 Y Y r 4
4 B B r 4
4 Z B r 5
4 _ _ l 7
5 Z B r 6
5 _ _ l 7
6 Z Z l 7
6 _ _ l 8
7 B Z l 7
7 Y Y l 7
7 A X l 7
7 $ $ r 12
8 B Z l 8
8 Y Y l 8
8 A X l 8
8 $ $ r 9
9 X X r 9
9 Y Y r 9
9 Z Z r 10
10 Z X r 11
11 Z Z r 11
11 _ _ l halt
12 X X r 12
12 Y Y r 12
12 Z Z r 12
12 _ _ l halt
Copy this code and then paste it to http://morphett.info/turing/turing.html
From advanced options, set initial state to 1 from 0.
Do not forget to add a "$" to beginning of every input.

Printing a 2D pattern

I discovered this pattern and decided to print it.
1 2 5 10 17
3 4 7 12 19
6 8 9 14 21
11 13 15 16 23
18 20 22 24 25
Rule here is to move from (0,0) to (0,1) to (1,0) to (1,1) to (0,2) to (2,0) to (1,2) to (2,1) to (2,2) and so on upto NxN matrix.
I have a very complex approach for printing it. Is there any easy way to print this pattern?
Update: Added one more row and column
It seems like the general rule is as follows:
Given a position as a tuple (n, m), the next position is
(n+1, 0), if n = m
(m, n), if n > m
(m, n+1), if n < m
In Python:
def next_pos(n, m):
if n == m: return (n+1, 0)
if n > m: return (m, n)
if n < m: return (m, n+1)
Example:
N = 5
n, m = (0, 0)
matrix = [[None for _ in range(N)] for _ in range(N)]
for x in range(N*N):
matrix[m][n] = x+1
n, m = next_pos(n, m)
Result:
1 2 5 10 17
3 4 7 12 19
6 8 9 14 21
11 13 15 16 23
18 20 22 24 25
Here is a Python solution that first extends every row by every other number starting with 1 more than the last perfect square, and then adds a new row, which consists of every other number starting with 2 more than the last perfect square, along with the final entry (which is the next perfect square in the sequence):
def expandSquare(square):
n = len(square[0])
for i, row in enumerate(square):
row.append(n**2 + 1 + 2*i)
square.append([k for k in range(n**2 + 2, (n+1)**2,2)] + [(n+1)**2])
def makeSquare(n):
square = [[1]]
for i in range(1,n): expandSquare(square)
return square
def pprint(square):
print('\n'.join('\t'.join(str(i) for i in row) for row in square))
For example,
>>> pprint(makeSquare(5))
1 2 5 10 17
3 4 7 12 19
6 8 9 14 21
11 13 15 16 23
18 20 22 24 25

Merge two text files line by line in Ruby

I'm trying to figure out how to merge two text files line by line. The letters file contains letters in a column A to I. Numbers contains numbers in a column from 1 to 9. This is what I have so far:
file='C:\\Users\\USERNAME\\Desktop\\numbers.txt'
f = File.open(file, "r")
f.each_line { |line|
dile='C:\\Users\\USERNAME\\Desktop\\letters.txt'
d = File.open(dile, "r")
d.each_line { |dine|
this = line + dine
print this
}
}
But my results are like this:
1
A
1
B
1
C
1
D
1
E
1
F
1
G
1
H
1
I
1
J2
A
2
B
2
C
2
D
2
E
2
F
2
G
2
H
2
I
2
J3
A
3
B
3
C
3
D
3
E
3
F
3
G
3
H
3
I
3
J4
A
4
B
4
C
4
D
4
E
4
F
4
G
4
H
4
I
4
J5
A
5
B
5
C
5
D
5
E
5
F
5
G
5
H
5
I
5
J6
A
6
B
6
C
6
D
6
E
6
F
6
G
6
H
6
I
6
J7
A
7
B
7
C
7
D
7
E
7
F
7
G
7
H
7
I
7
J8
A
8
B
8
C
8
D
8
E
8
F
8
G
8
H
8
I
8
J9
A
9
B
9
C
9
D
9
E
9
F
9
G
9
H
9
I
9
J10A
10B
10C
10D
10E
10F
10G
10H
10I
10J
When what I really want is something like this:
1A
2B
3C
4D
5E
6F
7G
8H
9I
Anyone have any idea how to do this?
f1, f2 = [
'C:\\Users\\USERNAME\\Desktop\\numbers.txt',
'C:\\Users\\USERNAME\\Desktop\\letters.txt'
]
File.readlines(f1).map(&:chomp)
.zip(File.readlines(f2).map(&:chomp))
.map(&:join)
or, without double chomping:
File.readlines(f1).zip(File.readlines(f2))
.map(&:join)
.map { |s| s.gsub /#$//, '' }
Its because each line already carries line feed \n. Try using chomp:
this = line.chomp + dine.chomp
Like a #mudasobwa answer
=> File.readlines('num').zip(File.readlines('let')).flat_map { |x| x.map(&:chomp!).join }
=> [
[0] "1A",
[1] "2B",
[2] "3C",
[3] "4D",
[4] "5E"
]
just without double chomp

How to compute a natural join??? 5

Table R (A, C) contains the following entries:
A C
3 3
6 4
2 3
3 5
7 1
Table S (B, C, D) following
B C D
5 1 6
1 5 8
4 3 9
Calculate the natural join of R and S. Which of the lines would be the result? Each resulting string has the following schema (A, B, C, D).
Please help!!!
Got the answer by looking at this. So your answer should be: {(3,4,3,9),(2,4,3,9),(3,1,5,8),(7,5,1,6)}
A B C D
3 4 3 9
2 4 3 9
3 1 5 8
7 5 1 6

How to sort dataframe in R with specified column order preservation?

Let's say I have a data.frame
x <- data.frame(a = c('A','A','A','A','A', 'C','C','C','C', 'B','B','B'),
b = c('a','c','a','a','c', 'd', 'e','e','d', 'b','b','b'),
c = c( 7, 3, 2, 4, 5, 3, 1, 1, 5, 5, 2, 3),
stringsAsFactors = FALSE)
> x
a b c
1 A a 7
2 A c 3
3 A a 2
4 A a 4
5 A c 5
6 C d 3
7 C e 1
8 C e 1
9 C d 5
10 B b 5
11 B b 2
12 B b 3
I would like to sort x by columns b and c but keeping order of a as before. x[order(x$b, x$c),] - breaks order of column a. This is what I want:
a b c
3 A a 2
4 A a 4
1 A a 7
2 A c 3
5 A c 5
6 C d 3
9 C d 5
7 C e 1
8 C e 1
11 B b 2
12 B b 3
10 B b 5
Is there a quick way of doing it?
Currently I run "for" loop and sort each subset, I'm sure there must be a better way.
Thank you!
Ilya
If column "a" is ordered already, then its this simple:
> x[order(x$a,x$b, x$c),]
a b c
3 A a 2
4 A a 4
1 A a 7
2 A c 3
5 A c 5
6 B d 3
9 B d 5
7 B e 1
8 B e 1
11 C b 2
12 C b 3
10 C b 5
If column a isn't ordered (but is grouped), create a new factor with the levels of x$a and use that.
Thank you Spacedman! Your recommendation works well.
x$a <- factor(x$a, levels = unique(x$a), ordered = TRUE)
x[order(x$a,x$b, x$c),]
Following Gavin's comment
x$a <- factor(x$a, levels = unique(x$a))
x[order(x$a,x$b, x$c),]
require(doBy)
orderBy(~ a + b + c, data=x)

Resources