- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在尝试在 Ruby 中实现 Luhn 算法,但不断收到错误消息,指出无法将 nil 合并到 Fixnum 中。
Luhn 算法应该:
从倒数第二个数字开始,每隔一个数字加倍,直到达到第一个数字
将所有未碰过的数字和翻倍的数字相加(翻倍的数字需要拆开,10变成1+0)
如果总数是 10 的倍数,则您收到了有效的信用卡号!
这是我的:
class CreditCard
def initialize (card_number)
if (card_number.to_s.length != 16 )
raise ArgumentError.new("Please enter a card number with exactly 16 integars")
end
@card_number = card_number
@total_sum = 0
end
def check_card
@new_Array = []
@new_Array = @card_number.to_s.split('')
@new_Array.map! { |x| x.to_i }
@new_Array.each_with_index.map { |x,y|
if (y % 2 != 0)
x = x*2
end
}
@new_Array.map! {|x|
if (x > 9)
x = x-9
end
}
@new_Array.each { |x|
@total_sum = @total_sum + x
}
if (@total_sum % 10 == 0)
return true
else
return false
end
end
end
最佳答案
在你的部分
@new_Array.each_with_index.map { |x,y|
if (y % 2 != 0)
x = x*2
end
}
更改不是永久性的。此外,正如 Victor 所写,如果测试失败,你的@new_Array 将被填充为 nils。最后
if (@total_sum % 10 == 0)
return true
else
return false
end
你可以直接写
@total_sum % 10 == 0
因为 ruby 方法的最后一行已经是一个返回。我找到了一个 slightly different algorithm 并在这里实现了它:
# 1) Reverse the order of the digits in the number.
# 2) Take the first, third, ... and every other odd digit in the reversed digits
# and sum them to form the partial sum s1
# 3) Taking the second, fourth ... and every other even digit in the reversed digits:
# a) Multiply each digit by two (and sum the digits if the answer is greater than nine) to form partial sums for the even digits
# b) Sum the partial sums of the even digits to form s2
# 4) If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test.
def luhn n
s = n.to_s.reverse
sum=0
tmp=0
(0..s.size-1).step(2) {|k| #k is odd, k+1 is even
sum+=s[k].to_i #s1
tmp = s[k+1].to_i*2
tmp = tmp.to_s.split(//).map(&:to_i).reduce(:+) if tmp>9
sum+=tmp
}
sum%10 == 0
end
[49927398716, 49927398717, 1234567812345678, 1234567812345670].each {|num|
puts "%20s %s" % [num, luhn(num)]
}
# 49927398716 true
# 49927398717 false
# 1234567812345678 false
# 1234567812345670 true
希望这对您有所帮助。
关于ruby - Ruby 中的 Luhn 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18138907/
我想了解其中的逻辑,以便我可以在 java 中实现这个算法。我想计算有效的十六进制 IMEI 号码的校验位。例如 - 6C4BFFC0000004请帮我算法。我试图在谷歌中找到解决方案,但我无法使用这
enter image description here 谁能解释一下这个程序。这个程序是我们老师给出的,他说这是关于 luhn 算法的,我找到了它是什么,但我无法理解该程序。 最佳答案 读完维基百科
大家好,我是编程界的新手。对于学校练习题,我得到了以下文本,我想将其转换为代码。我已经花了几个小时在上面,但似乎仍然无法弄清楚,但我决心学习这个。我目前遇到错误 line 7, in if i
我对编程非常陌生。我根据以下说明实现了信用卡验证程序。 让输入成为输入。 反向输入。 将输入的所有奇数位置(即索引 1、3、5 等)乘以 2。如果这些相乘项中的任何一项大于 9,则减去 9。 将输入的
排除校验位,luhn 算法的最小长度是多少? 我的想法是,它适用于任何大于 2 位的数字(同样,不包括校验位)。 我问的原因是:如果我从右到左遍历数字中的所有数字。这会导致我的 luhn 验证中的 i
在我的应用程序中,我想检查用户是否输入了我使用 LUHN 算法的有效卡号。我已将其创建为方法并在主 Activity 中调用。但即使我提供有效的卡号,它也会显示无效。在输入卡号时,我在中间留了空格,我
一切看起来都很好,似乎遵循 Luhn 的算法,但是当我输入我自己的信用卡号或这个应该有效的样本号:4388576018410707 时,它仍然无效... 谁能找到问题所在? #include int
我正在尝试用 JAVA 制作一个应用程序来检查用户输入的信用卡号是否有效。 以下是模数 10 检查的步骤: 第一步:从右到左每隔两个数字加倍。如果数字加倍得到两位数,则将这两位数相加得到一位数。 第
我一直在试图找出验证信用卡的 Luhns 方法,但我似乎无法弄清楚。我需要使用方法而不能使用数组,所以我完全被难住了。 这是 Luhns 检查: 信用卡号必须包含 13 到 16 位数字。它必须从以下
它应该使用 luhn 检查来告诉我一张卡是有效还是无效4388576018402626 无效4388576018410707 有效但它一直告诉我一切都是无效的:/任何关于做什么或去哪里寻找的建议都会很
我昨天完成了将这些函数组合在一起,它基本上是 Luhn 算法的实现。第一个函数应该接受一个整数和数组作为输入,然后将整数拆分为数字以填充数组。第二个应该检查数字是否有效并返回 1 或 0。我的问题是,
我希望有人能帮助我解决这个问题。我是一个彻底的 C 新手。 这是关于 C 类(class)的学校作业(只是普通的旧 C,而不是 C# 或 C++),教授坚持认为我们唯一允许使用的编译器是 Borlan
我有这段代码,由于某些原因,我在 for 循环的每次迭代中都会得到一个非常大的数字,但我无法弄清楚其中的逻辑。 #include #include #include #include int
我使用以下脚本来验证在表单中输入的卡详细信息。我想为此添加一个功能,以便提醒使用激光卡的访客我们不接受它们。 Laser的起始数字是6304,6706,6771&6709 function Calcu
我正在尝试使用此代码在 Java 中测试示例代码是否为有效的信用卡号(使用 Luhn algorithm )。我哪里做错了?它接受一个由 16 个一位数组成的数组。任何帮助将非常感激。谢谢! priv
我试图让某人输入信用卡号并通过 Luhn Check 验证它是否有效。我希望能够检查他们是否将整个卡号作为一个大字符串输入,或者是否在其中放置了空格。在我的函数验证中,尽管我不断收到一条错误消息,指出
我编写了一个工作代码来使用 luhn 算法检查信用卡是否有效: class CreditCard def initialize(num) @@num_arr = num.to_s.spli
我有一个 luhn 算法,我正在尝试按照维基百科上的算法步骤进行操作,并且它适用于他们提供的示例。我认为这是正确的。但它不适用于我的任何个人卡。也没有我在寻找解决方案时发现的任何测试值。 我已经看到使
我是一名新的编程学生,我正在尝试用 Java 编写一个算法来执行以下操作: 计算信用卡号的每隔一个数字(从倒数第二个数字开始)并将每个数字乘以 2。 取结果整数并将所有数字加在一起(即,如果我得到 1
我正在尝试实现 Luhn 算法来检查信用卡号的有效性。为此,每隔一个数字需要乘以 2,如果结果 >9,则将其替换为数字之和。 def luhn_check(creditcardnr): """ che
我是一名优秀的程序员,十分优秀!