Groovy: Iterate and multiply - list

I want to multiply every item in a list. But this example just returns every item:
list = 1..10
println list.each{it*2}

Use collect().
i.e.
list = 1..10
println list.collect{it*2}
Gives
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Only the syntax of Groovy is pegged to be standardized, not the default methods, so different implementations of Groovy have different names for the specific method to use.
In Codehaus Groovy (v 2.1.9 at groovy.codehaus.org) use collect...
list = 1..10
println list.collect{it*2}
In Real Groovy (v 0.10.0 at realgroovy.codeplex.com) use the more standardly named map...
test{
is [1,2,3,].map{it*7} == [7,14,21]
}
I'm not sure how GrooScript (at www.grooscript.org) does it.

Related

Python 3 list with range and other individual numbers

I need to make a list of numbers. These numbers represent binary masks. The first 100 or so masks are all included in this range. In the next group of masks only certain masks are included. I need a list similar to the following.
[1,2,3,5,6,7,8,9,10,30,34,48,53,62]
Can I do something like [range(1,10),30,34,48,53,62]
or do I need to create my list using range(1,10) and then append the next list to it?
Thanks
Python 3 actually allow you to build a list literal prepending an * to any iterable objects - which are in turn expanded in place:
>>> [1,2, *range(10), *range(2)]
[1, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
If you need this n older Pythons, or if you'd prefer to keep readability for people not too proeficient in Python who might have to walk through your code, an option is just to concatenate your different list fragments using the + operator:
a = list(range(1,10)) + [ 30,34,48,53,62]
Looks like I had to use the list(range(1,10)+[47,34,57]
solution

Creating an iterator to brute force

Hi so I am trying to write a function hack() that takes no arguments and returns an iterator over all possible passwords.
Basically, I have to user an iterator to brute force a password. All I know from the question is that:
The password consists of three segments.
The first segment of the password is a concatenation of two words in the following list of words: ["hello", "BEGONE", "dog", "MrCool"]
The middle segment is "Donkey20"
And the last segment consists of two digits that together (i.e. 1 and 7 = 17), are no larger than 38. (And at least 0)
My method of thinking is this:
Find all the possible combinations of POSSIBLE_WORDS by using permutations(). (Find all segment 1 possibilities)
For each combination in the new list, add "Donkey20" to the end. (For example: helloBEGONEDonkey20)
Find all possible combinations between the elements in THAT list and POSSIBLE_NUMBERS.
Create an iterator that iterates over all these possible passwords, and return it
My current code only is able to do step 2, but instead of Donkey20 being at the end of each combination it's at the start. (e.g. Donkey20helloBEGONE instead of helloBEGONEDonkey20)
POSSIBLE_WORDS = ["hello", "BEGONE", "dog", "MrCool"]
MIDDLE = "Donkey20"
possible_numbers1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
possible_numbers2 = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]
possible_numbers3 = [29, 30, 31, 32, 33, 34, 35, 36, 37, 38]
POSSIBLE_NUMBERS = possible_numbers1 + possible_numbers2 + possible_numbers3
from itertools import permutations, combinations
def hack():
first_words = [''.join(word) for word in permutations(POSSIBLE_WORDS, 2)]
first_words.append(MIDDLE)
first_half = [''.join(word) for word in permutations(first_words, 2)]
Any way to fix this issue? How do I finish the remainder of the steps? Is there a different approach I could use to code this program?
Any help would be very much appreciated!
First of all, there is no reason to build up POSSIBLE_NUMBERS like that. Just use range(39) or list(range(39)) instead.
Your intention in
first_words.append(MIDDLE)
was almost certainly not to tack the single word "Donkey20" onto the end of the list of all possible first parts but was instead to tack it onto the end of each first word. I think that your intention in that part of the code can be better expressed by getting rid of that line as well as the following line and just use the single line
first_half = [word + MIDDLE for word in first_words]
When you are putting together the final possible passwords, you are going to need to turn the numbers into strings. Doubtless you already know about str() but that function has the drawback that str(1) is '1' whereas you probably want '01'. In this context, you might want to use format() since format(1,'02') returns '01'.
This should be enough of a hint to get you going. Since this seems to be homework I don't want to say any more.

Scala error: value sort is not a member of List

Following a simple example at http://www.simplyscala.com/ I get:
scala> val lst=List(1,7,2,8,5,6,3,9,14,12,4,10)
lst: List[Int] = List(1, 7, 2, 8, 5, 6, 3, 9, 14, 12, 4, 10)
scala> lst.sort(_>_)
<console>:9: error: value sort is not a member of List[Int]
lst.sort(_>_)
What is wrong?
Thanks!
SimplyScala wasn't updated for a long long time: .sort was deprecated in 2.8.0 and cut out in latter versions. Instead you have to use sortWith method.
sortis actually not defined in class List. You should use sortWith in your case.
If you are running new version such as scala 2.11.8, you should check the document here scala 2.11.8 lib API.

Scan list in prolog

I want to scan a list in Prolog.
In particular, I want to write a predicate scan_list (list), and I want to make it check to see if the current element is a positive integer and if so print it.
Thank's.
If this is homework, be assured that the only way to learn any programming language is to practice it and think about the assignments. However, here is a version that might be, what you want
scan_list([]).
scan_list([H|T]) :- H > 0,!, print(H),nl,scan_list(T).
scan_list([_|T]) :- scan_list(T).
It works like that:
?- scan_list([1,-2,7,9,0,-1,14]).
1
7
9
14
yes
In SWI-Prolog there is include/3, e.g. you can write
?- include(<(0), [1, -2, 7, 9, 0, -1, 14, 0.8], L).
L = [1, 7, 9, 14, 0.8].
(Warning: this particular code accepts more numbers than positive integers.)

Appending Nested Lists in Python

I have the following question for homework
Define a function append lists that
takes a list of lists and returns a
new list containing the sublist
values. For example, append lists([[1,
2], [3, 4], [5]]) should return the
list [1, 2, 3, 4, 5] and append
lists([[1, 2], [3], [[4, 5]]]) should
return the list [1, 2, 3, [4, 5]].
I've tried various ways of creating this function in order to append the list so it gives the desired output to no avail so I came here looking for some help. I've found a few other ways of going about this online, but they use extensive methods that we haven't even dabbled in as of yet in my CPSC 121 class. We're limited to the basics in what we've learned.
Any help would be much appreciated!
By now, it is likely that the assignment is gone, but here is a solution:
def append_lists(lists):
output = []
for l in lists:
for e in l:
output.append(e)
return output
This appends each element of each list to the output of the function, which eliminates exactly one level of nesting in the elements.