Groovy List of Lists or 2D Array - collections

I am new to groovy and I am writing a program for reading numbers from an input file which has the following format
1
2 3
4 5 6
7 8 9 10
I wish to store them in a 2D array, how would I achieve it?
I have the following code so far for the read method
private read(fileName){
def count = 0
def fname = new File(fileName)
if (!fname.exists())
println "File Not Found"
else{
def input = []
def inc = 0
fname.eachLine {line->
def arr = line.split(" ")
def list = []
for (i in 1..arr.length-1) {
list.add(arr[i].toInteger())
}
input.add(list)//not sure if this is correct
inc++
}
input.each {
print it
//not sure how to reference the list
}
}
}
I am able to print the lists but I am not sure how to use the list of lists in the program (for performing other operations on it). Can anyone please help me out here?

On the input.each all you need is to iterate again in each item in the row. If it were a collection of unknown depth, then you'd need to stick to a recursive method.
Made a small change and removed the inc, since it is not needed (at least in the snippet):
fname = """1
2 3
4 5 6
7 8 9 10"""
def input = []
fname.eachLine { line->
def array = line.split(" ")
def list = []
for (item in array) {
list.add item.toInteger()
}
input.add list
}
input.each { line ->
print "items in line: "
for (item in line) {
print "$item "
}
println ""
}
Prints:
items in line: 1
items in line: 2 3
items in line: 4 5 6
items in line: 7 8 9 10
That is plain simple iteration. You can use #Tim's suggestion to make it more idiomatic in Groovy :-)

Related

How to take user input and insert it into an array?

New to Julia, trying to simply ask the user to choose 5 numbers and put it into an array and print the array. My output only says pick 5 numbers with "nothing" followed underneath. I cant seem to figure out why it wont read my inputs.
function ask()
lst = []
i = 0
println("pick 5 numbers to add to a list")
while i < 5
choice = readline
choice = push!(lst, choice);
i += 1
end
end
println(ask())
You were assigning function reference to list elements rather than calling the function.
This should be:
function ask()
lst = String[]
i = 0
println("pick 5 numbers to add to a list")
while i < 5
choice = readline()
choice = push!(lst, choice);
i += 1
end
lst
end
If you want numbers rather than Strings the last line could be parse.(Int, lst) or you could add this conversion near readline
Note that if you do not plan to introduce some error checking etc. this all code could be simply written as:
println("pick 5 numbers to add to a list")
lst = [parse(Int, readline()) for _ in 1:5]

Recursion and return of a index of array using recursion

can someone explain how variable ans return the index of an array if my array is 9 8 10 8 and I want to search 10 and in recursion ans will return 1 and then it will ad with ans+1 and return 2.
enter code here
int firstIndex(int input[], int size, int x)
{
if(size==0)
{
return -1;
}
if(input[0]==x)
{
return 0;
}
int ans=firstIndex( input+1, size-1, x);
if(ans!=-1)
{
return ans+1;
}
else
{
return ans;
}
}
Ok, so the function when it receive the first call, the vector looks like this:
[9 8 10 8]. Then it compares 9 with 10 (the element in search). Since they are not equals, the function makes a recursive call, but with a new vector, [8 10 8]. in that moment we have in our stack the first call. Let's call it C1.
Then it compares 8 with 10 (the element in search). Since they are not equals, the function makes a recursive call, but with a new vector, [10 8]. in that moment we have in our stack the second and first call. Let's call it C2 C1.
Then it compares 10 with 10 (the element in search). Since they are equals, the function makes returns 0. So in our stack, the C2 function receive a 0 as a result. Since it is different to -1, then it returns 1 to the C1 call. Since 1 is not equal to -1, it returns 2 leaving the correct result.

TCL recursively call procedure

I'm a beginner at TCL and while trying to build the GCD algorithm I ran into some problems I'd like some help with:
how can I call a proc inside a proc recursively like so
proc Stein_GCD { { u 0 } { v 0 } } {
if { $v == 0 } {
puts "$u\t\t$v\t\t$v"
}
if { [expr { $v % 2 } && { $u % 2 } ] == 0 } {
return [expr 2 * ${Stein_GCD 1 0} ]
}
}
set a [Stein_GCD 2 2 ]
puts $a
as you can see, I made the proc to evaluate GCD(the code does not make any sense because I'm trying to solve an example issue), and I'm trying to recursively call the proc again to continue evaluating(notice that I made an if statement that can understand the Stein_GCD 1 0 call, yet the tcl 8.6.6 online EDA emulator says:
can't read "Stein_GCD 1 0": no such variable
while executing
"expr 2 * ${Stein_GCD 1 0} "
(procedure "Stein_GCD" line 5)
invoked from within
"Stein_GCD 2 2 "
invoked from within
"set a [Stein_GCD 2 2 ]"
(file "main.tcl" line 7)
Can you tell me how to efficiently recursively call a proc, and where was my mistake?
will gladly provide more info in the case I did a bad job at explaining.
The error can't read "Stein_GCD 1 0": indicates that you are treating the data as a single string instead of separate arguments. The problem line:
return [expr 2 * ${Stein_GCD 1 0} ]
is not written correctly. ${Stean_GCD 1 0} is not a variable.
You should have:
return [expr 2 * [Stein_GCD 1 0] ]
You want the result from Stein_GCD 1 0, so the brackets should be used.

Go: pop from a map

Is there an existing function where we can pop a (key,value) pair from a map in GO? I use the word pop instead of remove because a pop would re-arrange the elements after the index where the (key,value) was removed.
As an example the following code:
package main
import "fmt"
func main() {
mapp := make(map[int]int)
fmt.Println("before removal:")
for i := 1; i < 7; i++ {
mapp[i] = i
}
fmt.Println(mapp)
delete(mapp, 2)
fmt.Println("\nafter the removal:")
for i := 1; i < 7; i++ {
fmt.Println(i, mapp[i])
}
}
Produces the following output:
before removal:
map[1:1 2:2 3:3 4:4 5:5 6:6]
after the removal:
1 1
2 0
3 3
4 4
5 5
6 6
We notice that index location 2 is empty. I would like the output to be the following:
before removal:
map[1:1 2:2 3:3 4:4 5:5 6:6]
after the removal:
1 1
2 3
3 4
4 5
5 6
Is this functionality already in Go or would I have to implement it?
I think that you are misunderstanding what a map is and how it works. You should not see it as an "array with gaps", but as a classic hash table.
And to answer your question, when you use delete(), the value is deleted from the map, the problem is how you iterate over the "values" of the map.
To help you understand:
mapp := make(map[int]int)
fmt.Println(2, mapp[2])
will print
2 0
Why ? Simply because when the requested key doesn't exist, we get the value type's zero value. In this case the value type is int, so the zero value is 0.
So, you want to see if a key exists in the map before printing it and you have to use two-value assignment, like that:
for i := 1; i < 7; i++ {
if value, exists := mapp[i]; exists {
fmt.Println(i, value)
}
}
and it will print
1 1
3 3
4 4
5 5
6 6
Not really what you want, but the closer you can get directly with maps.
You can have a look at this blog post for more information and examples.
If you really want to have an array where you can remove values, see Verran's answer and use slices instead.
From the Go documentation:
When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next.
From this, it follows that there would be no way to automatically move a value up one position to fill a gap, since the key can be in a different iteration position each time you look at the values and theres no guarantee that the value mapped to 2 will slide up to 1.
If you want to do something like this, you will have to manually shift everything down one key value, something like:
for key := 2; key < len(map)-1; key++ {
map[key] = map[key+1]
}
Alternatively, you could use slices and if you know the index you need to "pop", create a new slice that omits the value:
value := slice[2]
slice = copy(slice[:2], slice[2+1:])

Why doesn't this "binding" code work as expected in JavaFX?

I am new to JavaFX. I am not able to understand why the code below doesn't work.
import javafx.util.Sequences;
def nums = [1..10];
var curr = 0;
var evenOrOdd = bind if (nums[curr] mod 2 == 0) "{nums[curr]} is an even number" else "{nums[curr]} is an odd number";
for (curr in [0..(sizeof nums -1)])
{
println("{evenOrOdd}");
}
I am getting
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
1 is an odd number
If I change the code to
import javafx.util.Sequences;
def nums = [1..10];
var curr = 0;
var evenOrOdd = bind if (nums[curr] mod 2 == 0) "{nums[curr]} is an even number" else "{nums[curr]} is an odd number";
for (i in [0..(sizeof nums -1)])
{
curr = i;
println("{evenOrOdd}");
}
I get the correct output:
1 is an odd number
2 is an even number
3 is an odd number
4 is an even number
5 is an odd number
6 is an even number
7 is an odd number
8 is an even number
9 is an odd number
10 is an even number
Clearly, the counter increment in the loop is not treated as a value change and the bound expression is not re evaluated.
Can anyone please explain the concept behind this behavior?
The for expression implicitly defines its iteration variable (that's why you didn't need to declare i in your second example). Even if there is already a variable with the same name, for will still create a new one for its scope. Your bind expression is bound to the curr variable outside of your for loop, not to the one inside your for loop. And the one outside of your loop doesn't change, so the bound expression will not change.
Example to demonstrate this behaviour of for:
var curr = 0;
var ousideCurrRef = bind curr;
println("Before 'for' loop: curr={curr}");
for (curr in [0..3])
{
println("In 'for' loop: curr={curr} ousideCurrRef={ousideCurrRef}");
}
println("After 'for' loop: curr={curr}");
This will print:
Before 'for' loop: curr=0
In 'for' loop: curr=0 ousideCurrRef=0
In 'for' loop: curr=1 ousideCurrRef=0
In 'for' loop: curr=2 ousideCurrRef=0
In 'for' loop: curr=3 ousideCurrRef=0
After 'for' loop: curr=0
Thus the curr outside the for loop won't change if you modify a variable of the same name inside the for loop.

Resources