Created
December 9, 2016 06:40
-
-
Save sujinleeme/4a442c66fb7d3a1410e00674d3ed7839 to your computer and use it in GitHub Desktop.
test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. the sum of digits for one credit card number | |
function sumCard(num) { | |
// get number and change to integer | |
var num = num.match(/\d/g).map(Number); | |
// find the sum of an array of numbers | |
var sum = num.reduce((a, b) => a + b, 0); | |
return sum | |
} | |
// 2. get credit card number has the last largest sum of digits | |
function largestCard(lst) { | |
// initialize values | |
var sumlst = []; | |
var max = 0; | |
var maxIndex = 0; | |
var maxCardNum = ''; | |
// as getting the sum values of credit card, new max value and its index is updated. | |
for(item in lst) { | |
var addItem = sumlst.push(sumCard(lst[item])); | |
if (sumlst[item] >= max){ | |
max = sumlst[item]; | |
maxIndex = item; | |
} | |
} | |
maxCardNum = lst[maxIndex] | |
return maxCardNum | |
} | |
//Input Data | |
var inputCard = largestCard(['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']); | |
//Outout Data | |
console.log(inputCard) | |
# -*- coding: utf-8 -*- | |
# For this challenge, we are interested in finding the credit card number whose digits sum to the largest number. If more than one has the same largest sum of digits, we want the last one in the list with that sum. | |
# Write a single function that takes one argument. That argument will be an array of credit card numbers. Assume the array can have any number of credit card numbers and each one is a string of digits and dashes. Your function should return the credit card number that has the largest sum of digits. | |
# Here is a sample array of credit card numbers: | |
# ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'] | |
# In the sample array above, the digits add up to 49, 81, 81, and 64 respectively. Since there are two which have the same sum, the function should return the last one with that sum, in this case '4252-278893-7978' | |
# Things to accomplish: | |
# Contain all variables and code needed within a function. | |
# Have that function take one argument which will be an array of credit card number strings. | |
# Determine the sum of digits for each credit card number. | |
# Determine which credit card number has the last largest sum of digits. | |
# Use a return statement to return the required card number in its’ original form. | |
def sumCard(num): | |
num = sum(map(int, [x for x in filter(str.isdigit, num)])) | |
return num | |
def largestCard(lst): | |
#make sum list | |
sumLst = [sumCard(x) for x in lst] | |
#find index of item which equal to maxVal | |
indices = [x for x in range(len(sumLst)) if sumLst[x] == max(sumLst)] | |
#get the last one in the list and get element value | |
last = lst[indices[-1]] | |
return last | |
#print code | |
print(largestCard(['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment