Damm algorithm in rust


#1

Hello All, First post here

I’m looking for help trying to code a damm algorithm using Rust.

Implementations in other languages

matrix = (
(0, 3, 1, 7, 5, 9, 8, 6, 4, 2),
(7, 0, 9, 2, 1, 5, 4, 8, 6, 3),
(4, 2, 0, 6, 8, 7, 1, 3, 5, 9),
(1, 7, 5, 0, 9, 8, 3, 4, 2, 6),
(6, 1, 2, 3, 0, 4, 5, 9, 7, 8),
(3, 6, 7, 4, 2, 0, 9, 5, 8, 1),
(5, 8, 6, 9, 7, 2, 0, 1, 3, 4),
(8, 9, 4, 5, 3, 6, 2, 0, 1, 7),
(9, 4, 3, 8, 6, 1, 7, 2, 0, 5),
(2, 5, 8, 1, 4, 3, 6, 7, 9, 0))
def checkdigit(input):
row = 0
for eachdigit in input:
row = matrix[row][int(eachdigit)]
return row
for number in inputrange:
output = checkdigit(str(number))
print str(number)+str(output)

My trouble starts with selecting elements from inside a defined 10x10 matrix.

I tried using rust by example 2.2 tuples Now I understand that by changing the value 0 to 1

println!(“long tuple first value: {}”, long_tuple.0);

to

println!(“long tuple first value: {}”, long_tuple.1);

The first element of the second tuple is selected.

But how can the second element of the first tuple or the second element of the second tuple be selected. I have not been able to find resources on tuple indexing explained well.

Thanks to anyone who reads this


#2

Since I was not able to share more than two links on the first post

My current code Damm Rust


#3

Welcome to Rust forum :sparkles:! Keep reading, as new user restrictions (two links max for instance) will be removed when you read more posts.

You may want to use an array instead of tuple.

    let damm = [[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
                [7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
                [4, 2, 0, 6, 8, 7, 1, 3, 5, 9],
                [1, 7, 5, 0, 9, 8, 3, 4, 2, 6],
                [6, 1, 2, 3, 0, 4, 5, 9, 7, 8],
                [3, 6, 7, 4, 2, 0, 9, 5, 8, 1],
                [5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
                [8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
                [9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
                [2, 5, 8, 1, 4, 3, 6, 7, 9, 0]];

Then you will be able to access values in it by doing this:

damm[1][2] (for instance)

Tuples don’t provide a random access, because their purpose is to be a data structure that can store multiple values of possibly different types.

This is unlike say tuples in Python which do provide random access, as Python is dynamically typed programming language. If you are worried about mutability, in Rust, mutability is not part of a type, but a binding (I used let here, not let mut), tuples are as mutable in Rust as other types are.


#4

Thanks a lot! I’ll have to do more reading on here. I’m going to give it a try and thanks for the good information I’m very new to using rust.


#5

I think damm.0.1 would access the second element of the first tuple, much like damm[0][1] would work on a matrix. Correction: it must be (damm.0).1


#6

Thanks a lot, this was what I couldn’t figure out now I’m onto building a state machine with a for loop

Thanks a lot of the help


#7

By the way, looking forward to seeing your implementation when you write it. I would be happy be suggest improvements to make the code more idiomatic.

(this post used to contain a reference to Rosetta Code, but then I realized this is actually Wikibooks which contained sample implementations… sorta unusual to see code implementations here in style of Rosetta Code, but whatever… actually, thinking about it, I could probably add Damm algorithm task to Rosetta Code)


#8

That would be really cool, I find the algorithm extremely interesting for use in inventory systems. If I can finish it I will show you and put it on github. hopefully I figure it out soon. I initially made a CLI and GUI program in Python that outputs the numbers on PNG images based on a user defined range ( for creating files used to etch tags quickly). I was trying to mimic the functionality of that program simply for learning purposes since I understand the algorithm well enough.