-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d475e5
commit a0ae1b7
Showing
7 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Instructions | ||
|
||
The [ISBN-10 verification process][isbn-verification] is used to validate book identification numbers. | ||
These normally contain dashes and look like: `3-598-21508-8` | ||
|
||
## ISBN | ||
|
||
The ISBN-10 format is 9 digits (0 to 9) plus one check character (either a digit or an X only). | ||
In the case the check character is an X, this represents the value '10'. | ||
These may be communicated with or without hyphens, and can be checked for their validity by the following formula: | ||
|
||
```text | ||
(d₁ * 10 + d₂ * 9 + d₃ * 8 + d₄ * 7 + d₅ * 6 + d₆ * 5 + d₇ * 4 + d₈ * 3 + d₉ * 2 + d₁₀ * 1) mod 11 == 0 | ||
``` | ||
|
||
If the result is 0, then it is a valid ISBN-10, otherwise it is invalid. | ||
|
||
## Example | ||
|
||
Let's take the ISBN-10 `3-598-21508-8`. | ||
We plug it in to the formula, and get: | ||
|
||
```text | ||
(3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 1 * 5 + 5 * 4 + 0 * 3 + 8 * 2 + 8 * 1) mod 11 == 0 | ||
``` | ||
|
||
Since the result is 0, this proves that our ISBN is valid. | ||
|
||
## Task | ||
|
||
Given a string the program should check if the provided string is a valid ISBN-10. | ||
Putting this into place requires some thinking about preprocessing/parsing of the string prior to calculating the check digit for the ISBN. | ||
|
||
The program should be able to verify ISBN-10 both with and without separating dashes. | ||
|
||
## Caveats | ||
|
||
Converting from strings to numbers can be tricky in certain languages. | ||
Now, it's even trickier since the check digit of an ISBN-10 may be 'X' (representing '10'). | ||
For instance `3-598-21507-X` is a valid ISBN-10. | ||
|
||
[isbn-verification]: https://en.wikipedia.org/wiki/International_Standard_Book_Number |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"keiravillekode" | ||
], | ||
"files": { | ||
"solution": [ | ||
"impl.mips" | ||
], | ||
"test": [ | ||
"runner.mips" | ||
], | ||
"example": [ | ||
".meta/example.mips" | ||
] | ||
}, | ||
"blurb": "Check if a given string is a valid ISBN-10 number.", | ||
"source": "Converting a string into a number and some basic processing utilizing a relatable real world example.", | ||
"source_url": "https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Determine if input string is valid ISBN. | ||
# | ||
# $a0 - input, pointer to null-terminated string | ||
# $v0 - output boolean | ||
|
||
# $t0 - 1 | ||
# $t1 - initially 10, later 11 | ||
# $t2 - '-' | ||
# $t3 - '0' | ||
# $t4 - 'X' | ||
# $t5 - weight | ||
# $t6 - pointer to current character | ||
# $t7 - current digit | ||
# $t8 - sum | ||
# $t9 - weighted sum | ||
|
||
.globl is_valid | ||
|
||
is_valid: | ||
li $t0, 1 | ||
li $t1, 10 | ||
li $t2, '-' | ||
li $t3, '0' | ||
li $t4, 'X' | ||
move $t5, $t1 # Initial weight | ||
move $t6, $a0 # Pointer to first character | ||
move $t8, $zero # Set sum to 0 | ||
move $t9, $zero # Set weighted sum to 0 | ||
j load_byte | ||
|
||
found_x: | ||
bne $t5, $t0, report_invalid | ||
move $t7, $t1 | ||
|
||
update_sums: | ||
add $t8, $t8, $t7 # Update sum | ||
add $t9, $t9, $t8 # Update weighted sum | ||
subi $t5, $t5, 1 # Update weight | ||
|
||
advance: | ||
addi $t6, $t6, 1 # Increment pointer | ||
|
||
load_byte: | ||
lb $t7, 0($t6) # Load current character | ||
beq $t7, $zero, report | ||
beq $t7, $t2, advance # Ignore hyphen | ||
beq $t5, $zero, report_invalid # Too many characters | ||
beq $t7, $t4, found_x | ||
blt $t7, $t3, report_invalid # Reject if below '0' | ||
sub $t7, $t7, $t3 # Convert digit to value | ||
blt $t7, $t1, update_sums # Accept if below 10 | ||
|
||
report_invalid: | ||
move $v0, $zero # Set result to false | ||
jr $ra | ||
|
||
report: | ||
bne $t5, $zero, report_invalid # Reject non-zero weight | ||
li $t1, 11 | ||
div $t9, $t1 # Divide weighted sum by 11 | ||
mfhi $t9 # Remainder | ||
seq $v0, $t9, $zero # 1 if remainder is zero, otherwise 0 | ||
jr $ra |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[0caa3eac-d2e3-4c29-8df8-b188bc8c9292] | ||
description = "valid isbn" | ||
|
||
[19f76b53-7c24-45f8-87b8-4604d0ccd248] | ||
description = "invalid isbn check digit" | ||
|
||
[4164bfee-fb0a-4a1c-9f70-64c6a1903dcd] | ||
description = "valid isbn with a check digit of 10" | ||
|
||
[3ed50db1-8982-4423-a993-93174a20825c] | ||
description = "check digit is a character other than X" | ||
|
||
[9416f4a5-fe01-4b61-a07b-eb75892ef562] | ||
description = "invalid check digit in isbn is not treated as zero" | ||
|
||
[c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec] | ||
description = "invalid character in isbn is not treated as zero" | ||
|
||
[28025280-2c39-4092-9719-f3234b89c627] | ||
description = "X is only valid as a check digit" | ||
|
||
[f6294e61-7e79-46b3-977b-f48789a4945b] | ||
description = "valid isbn without separating dashes" | ||
|
||
[185ab99b-3a1b-45f3-aeec-b80d80b07f0b] | ||
description = "isbn without separating dashes and X as check digit" | ||
|
||
[7725a837-ec8e-4528-a92a-d981dd8cf3e2] | ||
description = "isbn without check digit and dashes" | ||
|
||
[47e4dfba-9c20-46ed-9958-4d3190630bdf] | ||
description = "too long isbn and no dashes" | ||
|
||
[737f4e91-cbba-4175-95bf-ae630b41fb60] | ||
description = "too short isbn" | ||
|
||
[5458a128-a9b6-4ff8-8afb-674e74567cef] | ||
description = "isbn without check digit" | ||
|
||
[70b6ad83-d0a2-4ca7-a4d5-a9ab731800f7] | ||
description = "check digit of X should not be used for 0" | ||
|
||
[94610459-55ab-4c35-9b93-ff6ea1a8e562] | ||
description = "empty isbn" | ||
|
||
[7bff28d4-d770-48cc-80d6-b20b3a0fb46c] | ||
description = "input is 9 characters" | ||
|
||
[ed6e8d1b-382c-4081-8326-8b772c581fec] | ||
description = "invalid characters are not ignored after checking length" | ||
|
||
[daad3e58-ce00-4395-8a8e-e3eded1cdc86] | ||
description = "invalid characters are not ignored before checking length" | ||
|
||
[fb5e48d8-7c03-4bfb-a088-b101df16fdc3] | ||
description = "input is too long but contains a valid isbn" |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# | ||
# Test is_valid with some examples | ||
# | ||
# s0 - num of tests left to run | ||
# s1 - address of input string | ||
# s2 - address of expected output word | ||
# s3 - char byte | ||
# s4 - output word | ||
# | ||
# is_valid must: | ||
# - be named is_valid and declared as global | ||
# - read input address of string from a0 | ||
# - follow the convention of using the t0-9 registers for temporary storage | ||
# - (if it uses s0-7 then it is responsible for pushing existing values to the stack then popping them back off before returning) | ||
# - write boolean result to v0 | ||
|
||
.data | ||
|
||
# number of test cases | ||
n: .word 19 | ||
# input values (null terminated) & expected output values (word sized ints) | ||
ins: .asciiz | ||
"3-598-21508-8", | ||
"3-598-21508-9", | ||
"3-598-21507-X", | ||
"3-598-21507-A", | ||
"4-598-21507-B", | ||
"3-598-P1581-X", | ||
"3-598-2X507-9", | ||
"3598215088", | ||
"359821507X", | ||
"359821507", | ||
"3598215078X", | ||
"00", | ||
"3-598-21507", | ||
"3-598-21515-X", | ||
"", | ||
"134456729", | ||
"3132P34035", | ||
"3598P215088", | ||
"98245726788" | ||
|
||
outs: .word | ||
1, | ||
0, | ||
1, | ||
0, | ||
0, | ||
0, | ||
0, | ||
1, | ||
1, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0 | ||
|
||
failmsg: .asciiz "failed for test input: " | ||
expectedmsg: .asciiz ". expected " | ||
tobemsg: .asciiz " to be " | ||
okmsg: .asciiz "all tests passed" | ||
|
||
|
||
.text | ||
|
||
runner: | ||
lw $s0, n | ||
la $s1, ins | ||
la $s2, outs | ||
|
||
run_test: | ||
move $a0, $s1 # move address of input str to a0 | ||
jal is_valid # call subroutine under test | ||
move $v1, $v0 # move return value in v0 to v1 because we need v0 for syscall | ||
|
||
lw $s4, 0($s2) # read expected output from memory | ||
bne $v1, $s4, exit_fail # if expected doesn't match actual, jump to exit_fail | ||
|
||
scan: | ||
addi $s1, $s1, 1 # move input address on byte forward | ||
lb $s3, 0($s1) # load byte | ||
bne $s3, $zero, scan # until char null, continue loop | ||
|
||
addi $s1, $s1, 1 # move input address on byte past null | ||
|
||
addi $s2, $s2, 4 # move to next word in output | ||
sub $s0, $s0, 1 # decrement num of tests left to run | ||
bgt $s0, $zero, run_test # if more than zero tests to run, jump to run_test | ||
|
||
exit_ok: | ||
la $a0, okmsg # put address of okmsg into a0 | ||
li $v0, 4 # 4 is print string | ||
syscall | ||
|
||
li $v0, 10 # 10 is exit with zero status (clean exit) | ||
syscall | ||
|
||
exit_fail: | ||
la $a0, failmsg # put address of failmsg into a0 | ||
li $v0, 4 # 4 is print string | ||
syscall | ||
|
||
move $a0, $s1 # print input that failed on | ||
li $v0, 4 | ||
syscall | ||
|
||
la $a0, expectedmsg | ||
li $v0, 4 | ||
syscall | ||
|
||
move $a0, $v1 # print actual that failed on | ||
li $v0, 1 # 1 is print integer | ||
syscall | ||
|
||
la $a0, tobemsg | ||
li $v0, 4 | ||
syscall | ||
|
||
move $a0, $s4 # print expected value that failed on | ||
li $v0, 1 # 1 is print integer | ||
syscall | ||
|
||
li $a0, 1 # set error code to 1 | ||
li $v0, 17 # 17 is exit with error | ||
syscall | ||
|
||
# # Include your implementation here if you wish to run this from the MARS GUI. | ||
# .include "impl.mips" |