I am new to robot framework and trying to implement Luhn Algorithm.
I found the code in python and want to change it to Robot framework.
def cardLuhnChecksumIsValid(card_number):
""" checks to make sure that the card passes a luhn mod-10 checksum """
sum = 0
num_digits = len(card_number)
oddeven = num_digits & 1
for count in range(0, num_digits):
digit = int(card_number[count])
if not (( count & 1 ) ^ oddeven ):
digit = digit * 2
if digit > 9:
digit = digit - 9
sum = sum + digit
return ( (sum % 10) == 0 )
I started with robotscript and was stuck at statement if not (( count & 1 ) ^ oddeven ) can someone please help me convert the above code to robot script
is there a automated to change python code to robot ?
cardLuhnChecksumIsValid
[Arguments] ${ICCID1}
${num_digits} Get length ${ICCID1}
${odd_even} ${num_digits}
... AND ${1}
FOR ${Count} IN RANGE 0 ${num_digits}
${digit} Convert To Integer ${ICCID[${Count}]}
Have a look at on the Evaluate keyword in the BuiltIn library. You can use that to rewrite:
if not (( count & 1 ) ^ oddeven ):
digit = digit * 2
to:
${condition}= Evaluate not (( ${count} & 1 ) ^ ${oddeven} )
${digit}= Run Keyword If ${condition} Evaluate ${digit} * 2
You can rewrite the rest of the algorithm using the same approach. From Robot Framework 3.2 release you can also use Inline Python evaluation.
*** Test Cases ***
Check - ICCID
[Setup] setup
${ICCID} Card.get ICCID #get ICCID
${Result_Chksum} cardLuhnChecksumIsValid ${ICCID}
[Teardown] tearDown
*** Keywords ***
setup
No Operation
tearDown
No Operation
cardLuhnChecksumIsValid
[Arguments] ${ICCID_val}
${num_digits} Get length ${ICCID_val}
${oddeven}= Evaluate ( ${1} & ${num_digits} )
FOR ${Count} IN RANGE 0 ${num_digits}
${digit} Convert To Integer ${ICCID_val[${Count}]}
${condition}= Evaluate not (( ${Count} & 1 ) ^ ${oddeven} )
${digit}= Run Keyword If ${condition} Evaluate (${digit}*2)
... ELSE Set Variable ${digit}
${digit}= Run Keyword If (${digit} > 9) Evaluate (${digit} - 9)
... ELSE Set Variable ${digit}
${Sum}= Evaluate (${Sum}+${digit})
END
${result}= Evaluate (${Sum}%10)
[Return] ${result}
Related
Forgive me if this is basic but I'm new to Elixir and trying to understand the language.
Say I have this code
defmodule Test do
def mult([]), do: 1
def mult([head | tail]) do
IO.puts "head is: #{head}"
IO.inspect tail
head*mult(tail)
end
end
when I run with this: Test.mult([1,5,10])
I get the following output
head is: 1
[5, 10]
head is: 5
'\n'
head is: 10
[]
50
But I'm struggling to understand what's going on because if I separately try to do this:
[h | t] = [1,5,10]
h * t
obviously I get an error, can someone explain what I'm missing?
Your function breaks down as:
mult([1 | [5, 10]])
1 * mult([5 | [10]])
1 * 5 * mult([10 | []])
1 * 5 * 10 * mult([])
1 * 5 * 10 * 1
50
The '\n' is actually [10] and is due to this: Elixir lists interpreted as char lists.
IO.inspect('\n', charlists: :as_lists)
[10]
Consider the arguments being passed to mult on each invocation.
When you do Test.mult([1,5,10]) first it checks the first function clause; that is can [1,5,10] be made to match []. Elixir will try to make the two expressions match if it can. In this case it cannot so then it tries the next function clause; can [1,5,10] be made to match [head|tail]? Yes it can so it assigns the first element (1) to head and the remaining elements [5,10] to tail. Then it recursively calls the function again but this time with the list [5,10]
Again it tries to match [5,10] to []; again this cannot be made to match so it drops down to [head|tail]. This time head is 5 and tail is 10. So again the function is called recursively with [10]
Again, can [10] be made to match []? No. So again it hits [head|tail] and assigns head = 10 and tail = [] (remember there's always an implied empty list at the end of every list).
Last go round; now [] definitely matches [] so it returns 1. Then the prior head * mult(tail) is evaluated (1 * 10) and that result is returned to the prior call on the stack. Evaluated again head (5) * mult(tail) (10) = 50. Final unwind of the stack head (1) * mult(tail) (50) = 50. Hence the overall value of the function is 50.
Remember that Elixir cannot totally evaluate any function call until it evaluates all subsequent function calls. So it hangs on to the intermediate values in order to compute the final value of the function.
Now consider your second code fragment in terms of pattern matching. [h|t] = [1,5,10] will assign h = 1 and t = [5,10]. h*t means 1 * [5,10]. Since those are fundamentally different types there's no inbuilt definition for multiplication in this case.
My testcase needed to set two or more value in order to evaluate with ${value} but this syntax does not work!
I appreciate if anybody could guide me.
${status} = Evaluate ${value} != 11 || 5
When you use Evaluate the expression must be valid Python.
In your case you could use:
${status} = Evaluate ${value} != 11 or ${value} != 5
or
${status} = Evaluate ${value} not in [11, 5]
I am trying to encrypt a message but if I use the letter Z it gives an error. This is the error (it's Dutch):
Set x = WScript.CreateObject("WScript.Shell")
mySecret = InputBox("The code")
mySecret = StrReverse(mySecret)
x.Run "%windir%\notepad"
WScript.Sleep 1000
x.SendKeys encode(mySecret)
Function encode(s)
For i = 1 To Len(s)
newtxt = Mid(s, i, 1)
newtxt = Chr(Asc(newtxt)+3)
coded = coded & newtxt
Next
encode = coded
End Function
The docs for SendKeys state:
Brackets "[ ]" have no special meaning when used with SendKeys, but
you must enclose them within braces to accommodate applications that
do give them a special meaning (for dynamic data exchange (DDE) for
example).
Your encoding method:
>> WScript.Echo Chr(Asc("Z") + 3)
>> WScript.Echo Chr(Asc("X") + 3)
>>
]
[
>>
generates brackets for Z and X.
After further testing:
The nasty letters are not (uppercase) X ([) and Z (]) but their lowercase cousins:
docs:
To send brace characters, send the string argument "{{}" for the left
brace and "{}}" for the right one.
evidence:
>> set x = WScript.CreateObject("WScript.Shell")
>> x.sendkeys Chr(Asc("z") + 3)
>>
Error Number: 5
Error Description: Invalid procedure call or argument
>> x.sendkeys Chr(Asc("x") + 3)
>>
Error Number: 5
Error Description: Invalid procedure call or argument
>>
The docs for Julia indicate that a valid short-cut to writing out an if statement is the syntax
<cond> && <statement>
I've used this a lot for error messages, e.g. length(x) < N && error("x is too short"), and it works as expected. However, the following does not work:
x = 3
x < 4 && x = 5
I get an error of the form syntax: invalid assignment location. What is going on here?
What I'm trying to do is check if x is less than 4, and if it is, then set x to 5. Should I do the following?
if x < 4
x = 5
end
Is there a valid short-circuit method for this situation?
Your error is caused because the && operator has higher precedence than the assignment operator = so your line of code is executed as if you would write (x < 4 && x) = 5.
For a solution you have to add parantheses.
x < 4 && (x = 5)
See my code running in the browser
I want to write a recursive method function that takes a nonnegative integer n as input and returns the number of 1s in the binary representation on n. I am instructed to use the fact that this is equal to the number of 1s in the representation of n//2 (integer division), plus 1 if n is odd.
Usage:
>>> ones(0)
0
>>> ones(1)
1
>>> ones(14)
3
ok so this is the code I got so far, it still doesn't work though. it gives me 0 no matter what I input as n.
def numOnes(n):
# base case
if n==0:
print (0)
elif n ==1:
print (1)
# recursive case
else:
return numOnes(n//2)+numOnes(n%2)
Thank you
These elements you need to do it yourself:
if integer & 1 == 1: # & is the binary and. & 1 will give the lowest bit
# there is a 1 in the end
integer = integer // 2 # loose one bit in the end
# or
integer = integer >> 1 # loose one bit in the end
Tell me if you need more input.
Your code works for me:
>>> def numOnes(n):
# base case
if n==0:
return (0)
elif n == 1:
return (1)
# recursive case
else:
return numOnes(n//2)+numOnes(n%2)
>>> numOnes(0b1100011)
4
You use print instead of return for the two base cases. Fix that and it'll work:
In [2]: numOnes(15842)
Out[2]: 9
In [3]: bin(15842).count('1')
Out[3]: 9