www.jammni.de

Logo - Kleiner Drache
Login
Username:

Passwort:

Daten merken
Auto-Login
Registrieren
 
Online
niemand
 
Forumsuche
Suche nach:

Logo - DracheHaskell-Forum

  1 2 nächste Seite

john_almighty

Gepostet:
26.12.2005 12:50

help...in writing code  
given a list of ds of digits, consider the problam of determining a permutation es= magic ds of ds with property that for each k in the range 1<= k<=length es, the decimal number formed the first k digits of es is divisible by k. Formulate a starightforward definistion of magic


Please help me do this...I don't know german but if you write just the code for me it would be a great help....

Thank you and Merry Christmas to all of you

John
Zum Seitenanfang    
 
Jammni

Gepostet:
27.12.2005 11:18

   
Well, jacke is in the holidays, but i tell her, that she has a new post here ;-) So expect an answer in the late evening...
Zum Seitenanfang Homepage   ICQ    
 
Jacke

Gepostet:
28.12.2005 14:38

   
Hmm, its very difficult to understand. Can you give us an example for each function you have to write (input and output)?
And what is given - is it a list of digits (called ds) or sth. other?

Heres some code for generating all permutations:

perms :: Eq a => [a] -> [[a]]
perms [] = [[]]
perms xs = [ x:ps | x <- xs , ps <- perms (xs\\[x]) ]


A permutation of a list is a list with the same elements in a different order. The perms function returns a list of all permutations of a list.
The empty list has one permutation, itself. If xs is not empty, a permutation is given by picking an element x from xs and putting x at the front of a permutation of the remainder xs\\[x]. (The operation '\\' returns the difference of two lists: xs\\ys is the list xs with each element of ys removed, if its present.)

I hope, this will help a little bit ;-)
Zum Seitenanfang    
 
john_almighty

Gepostet:
28.12.2005 23:23

   
Thank yu for your reply

Actually this is all that is given.....i.e. this is the only question given...nothing is associated with it...so one can assume any i/p he wants



Your reply has helped quite a bit but even I am confused with what to write for this question...


Happy new year

regards

John
Zum Seitenanfang    
 
Jacke

Gepostet:
29.12.2005 00:42

   
Hehe, now i understand the problem ;-) But theres still one question open.
Should the formed number be divisible by the index k or by the digit that is at position k? I'll try to code this tomorrow.

However, could you write for i/p what it means? I'm not familiar with this abbreviation...
Zum Seitenanfang    
 
Jacke

Gepostet:
29.12.2005 14:18

   
isDigit c = c >= '0' && c <= '9'

isInteger :: String -> Bool
isInteger s = all isDigit s

help :: String ->[Int]
help s
| (isInteger s) =map digitToInt s
| otherwise = [0]

digitToInt :: Char -> Int
digitToInt c
| isDigit c = fromEnum c - fromEnum '0'
| c >= 'a' && c <= 'f' = fromEnum c - fromEnum 'a' + 10
| c >= 'A' && c <= 'F' = fromEnum c - fromEnum 'A' + 10
| otherwise = error "Char.digitToInt: not a digit"

--makes an integer out of a list of integers [1,2,3]-> 123
conc l (-1) res =res
conc (l:ls) k res= conc ls (k-1) (l*(10^k) + res)

--verifies, thats this is a correct permutation and returns this permutation
magichelp x [] k= x
magichelp x (l:ls) k
|((test `mod` k) == 0) = magichelp (x ++ [l]) ls (k+1)
|otherwise =[]
where test=conc (help (x++ [l])) ((length (help (x++ [l]))) -1) 0


--generates the permutations
perm [] = [[]]
perm (x:xs) = [ ps++[x]++qs | rs <- perm xs , (ps,qs) <- splits rs ]

splits []=[([],[])]
splits(y:ys)=([],y:ys):[(y:ps,qs)|(ps,qs)<-splits ys]

--gives back a correct permutation
magichelp2 []=[]
magichelp2 (l:ls)
| ((magichelp [] l 1) /= [])=l
| otherwise =magichelp2 ls

--the main function
magic liste=magichelp2 (perm liste)



use:
Main> magic ['1','2','3']
"123"

Main> magic "67"
"76"
Main>

Zum Seitenanfang    
 
john_almighty

Gepostet:
29.12.2005 23:26

   
thanks a lot...yeah that is how it should be ....thank you very much....by the way i have a few more questions and was wondering if you are willing to do a few more coding......

Hav a great new year

John
Zum Seitenanfang    
 
Jacke

Gepostet:
29.12.2005 23:59

   
you can post any other questions ;-) i will try my best...
Zum Seitenanfang    
 
john_almighty

Gepostet:
30.12.2005 01:09

   
Dear Jacke
Actually I am doing my undergrad in aberdeen univ and they have given us such difficult questions in the holiday assignment......I want to pass so I need your help

By the way...could you email me the answer coz if my professor by mistake sees it online in the public forum...i wil be kicked out of the university......

my email is john.youngalmighty@googlemail.com

i hope you understand


1) give haskell and type of definition "merge" that merges two ordered list

2) give type and definition of function "member " such that "member xs x " returns True if "x" is an element of "xs" and False otherwise.

thanks

Zum Seitenanfang    
 
Jacke

Gepostet:
30.12.2005 12:39

   
mergesort
http://www.csc.depauw.edu/~bhoward/courses/0203Spring/csc122/haskintro/

isinlist [] x= False
isinlist (l:ls) x
|(l==x) =True
|otherwise =isinlist ls x
use :
Main> isinlist "123" '3'
True
Main> isinlist "123" '0'
False
Main> isinlist [1,2,3] 3
True
Main>


your prof cant find the posts with google ...try it ;-)
Zum Seitenanfang    
 

  1 2 nächste Seite