Skip to content

Commit

Permalink
sol(rust): pset1/credit
Browse files Browse the repository at this point in the history
  • Loading branch information
jfvillablanca committed Nov 5, 2023
1 parent bd9c2e7 commit 8b4bad7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions rust/pset1/credit/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions rust/pset1/credit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "credit"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
1 change: 1 addition & 0 deletions rust/pset1/credit/README.md
17 changes: 17 additions & 0 deletions rust/pset1/credit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::io::{self, Write};

pub fn get_long(prompt: &str) -> u64 {
loop {
print!("{prompt}");
io::stdout().flush().unwrap();

let mut input_text = String::new();
match io::stdin().read_line(&mut input_text) {
Ok(_) => match input_text.trim().parse() {
Ok(input_long) => return input_long,
Err(_) => continue,
},
Err(_) => continue,
}
}
}
55 changes: 55 additions & 0 deletions rust/pset1/credit/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use credit::get_long;

fn main() {
let cc_number = get_long("Number: ");
let cc_num_digits = cc_number.to_string().len() as u64;

if luhn(cc_number) {
let leading_digits = cc_number / 10_u64.pow((cc_num_digits - 2) as u32);
if cc_num_digits == 15 {
if leading_digits == 34 || leading_digits == 37 {
println!("AMEX");
return;
}
}
if cc_num_digits == 16 {
if leading_digits >= 51 && leading_digits <= 55 {
println!("MASTERCARD");
return;
}
}
if cc_num_digits == 13 || cc_num_digits == 16 {
let leading_digits = cc_number / 10_u64.pow((cc_num_digits - 1) as u32);
if leading_digits == 4 {
println!("VISA");
return;
}
}
}
println!("INVALID");
}

fn luhn(cc_number: u64) -> bool {
let cc_string = cc_number.to_string();
let mut cumsum_one = 0;
let mut cumsum_two = 0;

for (i, val) in cc_string
.chars()
.rev()
.enumerate()
.map(|(i, x)| (i, x.to_digit(10).unwrap_or(0)))
{
if i % 2 != 0 {
let mut sum = 2 * val;
if sum >= 10 {
cumsum_one += sum % 10;
sum /= 10;
}
cumsum_one += sum;
} else {
cumsum_two += val;
}
}
(cumsum_one + cumsum_two) % 10 == 0
}

0 comments on commit 8b4bad7

Please sign in to comment.