How to print the second lowest repetitive element by count in Scala - list

Input: list = [23, 23, 25, 34, 34, 32, 34]
Here 34 occurs 3 times, whereas 23 occurs 2 times.
I need to print the second lowest value. In this case output should be 23
I'm not able to figure out how I can pass the list as an argument and get the required output.

Related

How to set different (if) parameters for the same number in a list?

I don't even know how to format this paragraph right haha
Hi there I'm completely new to Python.
I was wondering if I have a list
how do I take the output of a first if command and use on the next
Example
L= [23, 91, 0, -11, 4, 23, 49]
for i in L:
if(i > 10):
print(i * 30)
example
I also want to check if the output of this number is an even number if so (+6) -- for the output of that
if number is not equal to -11 I want to add 10.
So I got 23 > 10= (23*30 = 690) // 690 is an even number ( 690 + 6=696) // it not equal to -11 so (696+10= 706).
How do I do this for every number on the list? Excuse my lack of knowledge it's literally my first python class and my first exercises.
Is that what you want?
L= [23, 91, 0, -11, 4, 23, 49]
for i in L:
if i > 10:
num = i * 30
if num % 2 == 0:
num += 6
if i != -11:
num += 10
else:
pass
print(num)
output:
706
2746
706
1486

Powerball number generator

To win the Powerball lottery (an extremely unlikely event so don't waste your time) you have to pick six numbers correctly. The first five numbers are drawn from a drum containing 53 balls and the sixth is drawn from a drum containing 42 balls. The chances of doing this are 1 in 120,526,770.
The output needs to be in the form:
Official (but fruitless) Powerball number generator
How many sets of numbers? 3
Your numbers: 3 12 14 26 47 Powerball: 2
Your numbers: 1 4 31 34 51 Powerball: 17
Your numbers: 10 12 49 50 53 Powerball: 35
import random
#Powerball
print "Offical Powerball number generaor"
x = int(raw_input("How many sets of numbers? "))
z = range(1,42)
z1 = random.choice(z)
def list1():
l1=[]
n=1
while n<=5:
y = range(1,53)
y1 = random.choice(y)
l1.append(y1)
n +=1
print sorted(l1)
i=1
while i<=x:
# print "Your numbers: " + list1() + "Powerball: "+ str(z1)
print list1()
raw_input("Press<enter>")
My code's output goes on a infinite loop. I have to kill it. And the message is:
None
[2, 7, 22, 33, 42]
None
[15, 19, 19, 26, 48]
None
[1, 5, 7, 26, 41]
None
[7, 42, 42, 42, 51]
None
..... etc ....
while i<=x: - you never increment i, so it is stuck in your last loop...
To avoid such things and remove the noise of i+=1 lines in your code I suggest using for loops for i in range(x) and for n in range(5).
Better yet, the following expression can replace list1:
[random.choice(range(1,53)) for x in xrange(5)]
At least, that does the same as your code. But what you probably really want (to avoid the same ball being chosen twice) is:
random.sample( range(1,53), 5 )

How to count Combinations

I was wondering how i would go about counting combinations in a list. To be more precise i have a list that is comprised of smaller lists that are made up of 6 randomly chosen numbers and i want to count how many times each combinations occurs within the bigger list and then finally display the least occurring combination. So far i tried using Counter() but it seems it can't count lists.
here's an example of what i want to do:
list = [[1,2,3,4,5,6],[1,5,16,35,55,22],[1,2,3,4,5,6],[5,25,35,45,55,10],[1,5,16,35,55,22],[1,2,3,4,5,6],[9,16,21,22,23,6],[9,16,21,22,23,6]]
so after counting the combinations it should print the combination [5,25,35,45,55,10]
since it only occurred once in the list
FYI the list is going to randomly generated with around 1 billion combinations stored but given the range of numbers, there's only 175 million possible combinations
FYI 2 i'm extremely new to python
When you construct the Counter instance you can convert your lists to tuples; the latter are hashable, which is the property an object needs to be able to serve as a key of a dict.
>>> from collections import Counter
>>> l = [[1,2,3,4,5,6],[1,5,16,35,55,22],[1,2,3,4,5,6],[5,25,35,45,55,10],[1,5,16,35,55,22],[1,2,3,4,5,6],[9,16,21,22,23,6],[9,16,21,22,23,6]]
>>> c = Counter(tuple(e) for e in l)
>>> c
Counter({(1, 2, 3, 4, 5, 6): 3, (1, 5, 16, 35, 55, 22): 2, (9, 16, 21, 22, 23, 6): 2, (5, 25, 35, 45, 55, 10): 1})
>>> list(c.most_common()[-1][0])
[5, 25, 35, 45, 55, 10]

Numbers between a and b without their permutations

I've written a similar question which was closed I would like to ask not the code but an efficiency tip. I haven't coded but if I can't find any good hint in here I'll go and code straightforward. My question:
Suppose you have a function listNums that take a as lower bound and b as upper bound.
For example a=120 and b=400
I want to print numbers between these numbers with one rule. 120's permutations are 102,210,201 etc. Since I've got 120 I would like to skip printing 201 or 210.
Reason: The upper limit can go up to 1020 and reducing the permutations would help the running time.
Again just asking for efficiency tips.
I am not sure how you are handling 0s (eg: after outputting 1 do you skip 10, 100 etc since technically 1=01=001..).
The trick is to select a number such that all its digits are in increasing order (from left to right).
You can do it recursively. AT every recursion add a digit and make sure it is equal to or higher than the one you recently added.
EDIT: If the generated number is less than the lower limit then permute it in such a way that it is greater than or equal to the lower limit. If A1A2A3..Ak is your number and it is lower than limit), then incrementally check if any of A2A1A3...Ak, A3A1A2...Ak, ... , AkA1A2...Ak-1 are within limit. If need arises, repeat this step to with keeping Ak as first digit and finding a combination of A1A2..Ak-1.
Eg: Assume we are selecting 3 digits and lower limit is 99. If the combination is 012, then the lowest permutation that is higher than 99 is 102.
When the lower bound is 0, an answer is given by the set of numbers with non-decreasing digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 27, 28, 29, 33, 34, 35, 36, 37, 38, 39, 44, 45, 46, 47, 48, 49, 55, 56, 57, 58, 59, 66, 67, 68, 69, 77, 78, 79, 88, 89, 99, 111, 112...) that fall in the requested range.
This sequence is easily formed by incrementing an integer, and when there is a carry, replicate the digit instead of carrying. Exemple: 73 is followed by 73+1 = 74 (no carry); 79 is followed by 79+1 = 80 (carry), so 88 instead; 22356999 is followed by 22356999+1 = 22357000, hence 22357777.
# Python code
A= 0 # CAUTION: this version only works for A == 0 !
B= 1000
N= A
while N < B:
# Detect zeroes at the end
S= str(N)
P= S.find('0')
if P > 0:
# Replicate the last nonzero digit
S= S[:P] + ((len(S) - P) * S[P-1])
N= eval(S)
# Next candidate
print N
N+= 1
Dealing with a nonzero lower bound is a lot more tricky.

Represent sequence of tetrahedral numbers in Haskell

I've been wanting to learn some Haskell for a while now, and I know it and similar languages have really good support for various kinds of infinite lists. So, how could I represent the sequence of tetrahedral numbers in Haskell, preferably with an explanation of what's going on?
0 0 0
1 1 1
2 3 4
3 6 10
4 10 20
5 15 35
6 21 56
7 28 84
8 36 120
In case it's not clear what's going on there, the second column is a running total of the first column, and the third column is a running total of the second column. I'd prefer that the Haskell code retain something of the "running total" approach, since that's the concept I was wondering how to express.
You're correct, Haskell is really nice for doing things like this:
first_col = [0..]
second_col = scanl1 (+) first_col
third_col = scanl1 (+) second_col
first_col is an infinite list of integers, starting at 0
scanl (+) calculates a lazy running sum: Prelude docs
We can verify that the above code is doing the right thing:
Prelude> take 10 first_col
[0,1,2,3,4,5,6,7,8,9]
Prelude> take 10 second_col
[0,1,3,6,10,15,21,28,36,45]
Prelude> take 10 third_col
[0,1,4,10,20,35,56,84,120,165]
Adding to perimosocordiae's great answer, languages like Haskell are so slick they allow you to make an infinite list of infinite lists.
First lets define the operator that produces each successive row:
op :: [Integer] -> [Integer]
op = scanl1 (+)
As explained by perimosocordiae, this is just a lazy running sum.
We also need a base case:
tnBase :: [Integer]
tnBase = [0..]
So how do we get an infinite list of infinite lists of tetrahedral numbers? We iterate this operation on the base case, then the output produced by the base case, then that output...
tn = iterate op tnBase
iterate is in the Prelude, such functions can be found using hoogle and searching by name (if you have a good guess) or type signature (you generally know the signature of what you need). Source code is usually linked from the haddock documentation.
Presentation
(in case you're not comfortable with map, take, drop, and head)
This is all well and good, but rather useless if you don't know how to get passed the first infinite list to see the second, third, etc. There are plenty of options, for just getting a particular list you can drop the first few:
getNthTN n = head (drop n tn)
Getting the first few results of each list is probably more what you're looking for though:
printFirstFew n m = print $ take m (map (take n) tn)
Here map (take n) tn will take the first n values from each list of tetrahedral numbers while take m will limit our results to the first m lists.
And lastly, I like the awesome groom package for quick interactive playing with data:
> groom $ take 10 (map (take 10) tn)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45],
[0, 1, 4, 10, 20, 35, 56, 84, 120, 165],
[0, 1, 5, 15, 35, 70, 126, 210, 330, 495],
[0, 1, 6, 21, 56, 126, 252, 462, 792, 1287],
[0, 1, 7, 28, 84, 210, 462, 924, 1716, 3003],
[0, 1, 8, 36, 120, 330, 792, 1716, 3432, 6435],
[0, 1, 9, 45, 165, 495, 1287, 3003, 6435, 12870],
[0, 1, 10, 55, 220, 715, 2002, 5005, 11440, 24310],
[0, 1, 11, 66, 286, 1001, 3003, 8008, 19448, 43758]]