Assign specific number to letter - node.js

I'm working on a little text-based Blackjack game in NodeJS. I've got this array:
const ranks = Array('A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K');
The game picks a random number from this array and displays it. But, with letters you can't count. In Blackjack, the "J", "Q" and "K" are 10. The A is 1 OR 11.
I still want it to display to the user the letter, but it has to count with 10. So how can I assign this 10 (or 1/11) to the face-cards, but still display the letter.

The first way that I think of this is to define an array of objects rather than an array of Strings.
const ranks = [
{
actualValue: 11,
faceValue: 'A'
},
{
actualValue: 2,
faceValue: '2'
},
...
];
With this data structure you can do your math to get counts and also display whatever you want to display.

Related

Fill a dictionnary from a list

Dears,
Your support please to have the below target output..
The input is a list in which the number whith four digits (exp : '1368', , '1568', '1768') should b the key of output dictionary, and the number with one digit which follow the 4 digits numbers should be the values of those keys like below.
exp :
INPUT :
['1368', '1', '1368', '3', '1568', '1', '1568', '3', '1568', '2', '1768', '3', '1768', '2', '2368', '1', '2368', '3', '2368', '2']
OUTPUT
{'1368' :['1', '3'], '1568' :['1', '3','2'], '1768' :['3','2'], '2368' :['1','3','2'] }
/////////////////////////////////////////////////////
try this:
output = {}
for key in iterator:=iter(input):
value = next(iterator)
output.setdefault(key, []).append(value)
But I apologize - this is a "pythonism" - as we in Python tend to avoid having to deal with explicit indexes for sequences. There is no harm in the more readable:
output = {}
for index in range(0, len(input), 2):
key, value = input[i], input[i+1]
output.setdefault(key, []).append(value)
The "setdefault" method on the other hand is a valid "pythonism" and is equivalent to "if this key already exists in the dicionary, return it, otherwise, set it to this new value (the second argument), and return it΅ - and it avoids the need for an "if" statement and an additional line.

Number from a string of numbers-prolog

I would like to get a number within a string like
1100010 I would like to get the last two digits
How would I do that If I dunno the the length of the given String
forFun("",0).
forFun(Str,N) :- Str1 is Str - 1,
forFun(Str1,N1),
N is N1 + 1.
is that a possible code ?
First (unfortunately) we need to agree what we mean by "string". For many purposes the best and most portable representation is a list of "chars" (one-character atoms), though your Prolog might need to be told that this is really the representation you want:
?- set_prolog_flag(double_quotes, chars).
true.
Now, for example:
?- Number = "12345".
Number = ['1', '2', '3', '4', '5'].
To get the last two elements of a list, you can use the widely supported (though not standard) append/3 predicate. You can ask Prolog a question that amounts to "are there some list _Prefix (that I do not care about) and a two-element list LastDigits that, appended together, are equal to my Number?".
?- Number = "12345", LastDigits = [X, Y], append(_Prefix, LastDigits, Number).
Number = ['1', '2', '3', '4', '5'],
LastDigits = ['4', '5'],
X = '4',
Y = '5',
_Prefix = ['1', '2', '3'] ;
false.
The last two digits of this number are ['4', '5'].

python variable assignment index Error: list index out of range with pulp

Hi I don't understand how this error is generated. I have the same amount of constraints on the left side and same amount of element in the list on the right side. maybe I am missing a small sign or my logic is wrong. please help me to understand.
Machines = ["A", "B",]
Days= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"]
desire_num={"A":5, "B":2,}
week1={"Monday":1, "Tuesday":1, "Wednesday":1, "Thursday":1,
"Friday":1, "Saturday":1, "Sunday":1}
status_list=['1', '1', '0', '1', '0', '1', '1', '0', '0', '1', '0',
'1', '0', '0']
avail = pulp.LpVariable.dicts("var", ((machine, day) for machine in
Machines for day in Days), cat="Binary")
##---problem is here. I have 14 variables on the left and 14 elements in the list on the right. The error says list index out of range.
status_list_iterator = 0
for machine, day in avail:
self.prob += avail[machine, day] ==
status_list[status_list_iterator]
status_list_iterator+=1
thanks again for some clarification.
The rhs of a constraint should contain at least a numeric value (a boolean one is fine too). You are setting constraint of the form:
self.prob += avail[machine, day] == '1'
#or
self.prob += avail[machine, day] == '0'
You can either change the elements in status_list to be numeric values or do something like as follows:
for (machine, day), status in zip(avail, status_list):
prob += avail[(machine, day)] == int(status), "c_{}_{}".format(machine, day)

Going over a list to find which if any items repeat more than X times, then returning those items

I have a list:
my_list = ['2', '5', '7', '7', '5']
I need to be able to check if any item repeats X time in the list, and if so - which one(s). For instance, I'd like to check if any (and which) items repeat (2) times in the list above, in which case I would expect:
5, 7 # this can be in the form of a list, strings, or anything else.
What I have tried:
After looking over some previous posts on StackExchange, I first went ahead and used collections-counter (not sure if this is a good approach), like so:
repetition = collections.Counter(my_list)
What this returns is a dictionary, like so:
{'5': 2, '7': 2, '2': 1}
Now I still need to check which item(s) repeat twice. After some more searching, I ended up with this:
def any(dict):
repeating = []
for element in dict.values():
if element == 2:
(...)
I'm uncertain however of how to continue with thise code. Seems like I can only get the number of repetitions, in this '2' (ie. the value from the dictionary), but am unable to figure out a simple way for getting the Keys which have a value of 2.
Is there an easy way to do it? Or should I try a different approach?
Thank you.
You need to loop over the items of the dictionary so you have both the key and the value:
repeating = [key for key, value in repetition.items() if value >= 2]
I used a list comprehension here to do the looping; all keys that have a value of 2 or higher are selected.
Demo:
>>> from collections import Counter
>>> my_list = ['2', '5', '7', '7', '5']
>>> repetition = Counter(my_list)
>>> [key for key, value in repetition.items() if value >= 2]
['5', '7']

I am trying to initialize a matrix, but get the same row repeated

I am trying to initialize a matrix of a list of lists of characters.
aRow = [ '0', '0','0' ]
aGrid = [ aRow, aRow, aRow ]
After it appeared like one row repeated three times, I tried modifying the rows as follows and printing the result:
aGrid[1][0] = '1'
aGrid[2][0] = '2'
print( aGrid )
It looks like I am getting the third row three times.
[['2', '0', '0'], ['2', '0', '0'], ['2', '0', '0']]
Why?
In python, when you assign values you're really assigning references to the values. So this statement creates a list with the value ['0', '0', '0'] and assigns a reference to that value to aRow:
aRow = [ '0', '0','0' ]
And so this statement then creates a list with three references to the same list:
aGrid = [ aRow, aRow, aRow ]
In Python, lists are mutable values, so changes to the underlying value are reflected by all references to that value. So:
aGrid[1][0] = '1'
and:
aGrid[2][0] = '2'
Both change the first element of the underlying list that all three references in aGrid are referencing, and so the last change is the one you'll see.
Because you are. You need to copy the object if you want the contents to be different.
aGrid = [aRow, aRow[:], aRow[:]]

Resources