Advertisement
Guest User

Untitled

a guest
Jan 26th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.39 KB | None | 0 0
  1. let changeDigit(position:int)(newDigit: Nat)(password: Nat List):Nat List = //changes list element on "position" to "newDigit"
  2.  
  3.     let mutable passwordArray = Array.ofList(password)
  4.     let mutable passwordList = password
  5.     let mutable oldDigit = passwordArray.[position]
  6.     let mutable i = 10N-newDigit
  7.  
  8.     while i > 0N do
  9.         oldDigit <- (oldDigit-1N)
  10.         i <- (i-1N)
  11.     passwordArray.[position-1] <- oldDigit
  12.     passwordList <- List.ofArray(passwordArray)
  13.     passwordList
  14.  
  15. let rec crackPassword(password: Nat List)(secret: Secret) = //finds out correct digits one by one
  16.  
  17.     let mutable crackedPassword = password
  18.     let mutable stelle = 0
  19.     let mutable ziffer = 0N
  20.     let mutable oldP = 0N
  21.     let mutable oldD = 0N
  22.  
  23. // I don't know what I'm doing from here
  24.  
  25.     // try unlock crackedPassword secret with
  26.     // |WrongPassword(p, d) -> if p > oldP then
  27.     //                             crackedPassword <- (changeDigit stelle ziffer crackedPassword)
  28.     //                             oldP <- (oldP+1N)
  29.     //                             stelle <- (stelle+1)
  30.     //                             crackPassword crackedPassword secret
  31.     //                         elif d > oldD then
  32.  
  33. (*
  34. I'm writing a program to "crack" a password of numbers between 0-9 (represented as a list) using exception handling. The function unlock (password: Nat list) (s: Secret): String throws a WrongPassword(p, d) exception on a wrong password input, where p is the number of correct numbers in correct positions, and d the number of overall correct numbers. For now I have a function that determines the length of the password and fills the elements of the list with 10's. The changeDigit function takes a list and changes an element on a chosen position to a chosen number. I want to use the changeDigit function to find out the correct numbers one by one unsing the information from WrongPassword(p, d) but the implementation of the function confuses me a lot. I want the try the start with the first position in the list and increase the number by one everytime the exception is thrown until p increases by 1. I then want to continue with the next position in the list. A further optimization would be to use d to try all positions until p increases by 1. I'm trying to realise my idea in the crackPassword function, but I can't figure out how to write a working loop with the exception handling.
  35. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement