reset password
Author Message
tvo54
Posts: 7
Posted 14:24 Mar 10, 2019 |

Any tips on how to create ['a' ..] in python?

rabbott
Posts: 1649
Posted 14:28 Mar 10, 2019 |

Please elaborate on your question. What are you having trouble creating?

tvo54
Posts: 7
Posted 14:35 Mar 10, 2019 |

I know how to create a list containing a-z, but dont know how to create a list containing all the characters after that like {, (, \DEL, \128, \129, \130, Trying to figure out how to do this one:

 

rlePropRoundTrip :: [Int] -> Bool
rlePropRoundTrip ns = runLengthEncode xs == is
  where is = zip ['a' ..] $ map (\n -> n `mod` 100 + 1) ns
        xs = concatMap (\(i, n) -> replicate n i) is

Last edited by tvo54 at 14:37 Mar 10, 2019.
jpatel77
Posts: 44
Posted 15:03 Mar 10, 2019 |

I would create a generator function that takes an argument that is string, converts that into int representation using ord, and then considering that as a starting point, would yield incremented value converted back into char using chr, indefinitely.

You may then extract the indefinite sequence by calling next onto it.

Last edited by jpatel77 at 15:04 Mar 10, 2019.
jpatel77
Posts: 44
Posted 15:48 Mar 10, 2019 |

I was wondering if we could wrap the existing count with required modification. Toolz lib turned out to be more useful than I thought. Here's how I tried to make our own wrapper:

>>> from toolz.curried import *
>>> from itertools import count, islice

>>> myCount = compose(map(chr), count, ord)
>>> startFromA = myCount('A')
>>> print(list(islice(startFromA, 5)))    # first 5 from sequence

>>> ['A', 'B', 'C', 'D', 'E']

Edit: 'myCount' is more suitable than 'myRange' thus renamed it.

Last edited by jpatel77 at 23:33 Mar 10, 2019.
tvo54
Posts: 7
Posted 15:55 Mar 10, 2019 |

Hm, for some reason I get 'int' object is not iterable when trying to run your code...

jpatel77
Posts: 44
Posted 16:01 Mar 10, 2019 |

Did you import the toolz? I used curried version of everything. Try importing the following:

from toolz.curried import *
from itertools import count, islice

Edit: Just edited the code with required imports.

Last edited by jpatel77 at 16:11 Mar 10, 2019.
rabbott
Posts: 1649
Posted 16:06 Mar 10, 2019 |

To help me understand what you were after, I translated your original code into this.

charNumPairs :: Integral n => [n] -> [(Char, n)]          -- formerly is
charNumPairs = zip ['a' ..] . map (\n -> n `mod` 100 + 1)

repeatedCs :: [Int] -> [Char]                                       -- formerly xs
repeatedCs  = concatMap (\(c, n) -> replicate n c) . charNumPairs

runLengthEncode :: Eq a => [a] -> [(a, Int)]               -  copied from slide 50 of Amuse-Bouche
runLengthEncode [] = []
runLengthEncode (x:xs) = nextGroup x 1 xs
  where
    nextGroup e n [] = [(e, n)]
    nextGroup e n (y:ys)
      | e == y    =          nextGroup e (n + 1) ys
      | otherwise = (e, n) : nextGroup y  1      ys

rlePropRoundTrip :: [Int] -> Bool
rlePropRoundTrip ns = runLengthEncode (repeatedCs ns) == charNumPairs ns

This gives the result you want.

> repeatedCs [1 .. 5]
"aabbbccccdddddeeeeee"

> rlePropRoundTrip [1 .. 5]
True

 

Not sure where to go from here. Jay's suggestion for generating letters sounds like a good approach.

Last edited by rabbott at 16:13 Mar 10, 2019.
tvo54
Posts: 7
Posted 16:11 Mar 10, 2019 |

Ah, there we go. Okey, if i were to run this:

something dwd =  zip ['a' ..] $ map (\n -> n `mod` 100 + 1) dwd, where dwd = [1 up to 40], the output would be:

[('a',2),('b',3),('c',4),('d',5),('e',6),('f',7),('g',8),('h',9),('i',10),('j',11),('k',12),('l',13),('m',14),('n',15),('o',16),('p',17),('q',18),('r',19),('s',20),('t',21),('u',22),('v',23),('w',24),('x',25),('y',26),('z',27),('{',28),('|',29),('}',30),('~',31),('\DEL',32),('\128',33),('\129',34),('\130',35),('\131',36),('\132',37),('\133',38),('\134',39),('\135',40),('\136',41)]
 

And if i choose range up to 40 with Jays code, it would print out:

 

[('A', 2), ('B', 3), ('C', 4), ('D', 5), ('E', 6), ('F', 7), ('G', 8), ('H', 9), ('I', 10), ('J', 11), ('K', 12), ('L', 13), ('M', 14), ('N', 15), ('O', 16), ('P', 17), ('Q', 18), ('R', 19), ('S', 20), ('T', 21), ('U', 22), ('V', 23), ('W', 24), ('X', 25), ('Y', 26), ('Z', 27), ('[', 28), ('\\', 29), (']', 30), ('^', 31), ('_', 32), ('`', 33), ('a', 34), ('b', 35), ('c', 36), ('d', 37), ('e', 38), ('f', 39), ('g', 40), ('h', 41)]

 

Should it matter what is in the first index of the tuples for rlePropRoundTrip to work?

Last edited by tvo54 at 16:20 Mar 10, 2019.
rabbott
Posts: 1649
Posted 16:18 Mar 10, 2019 |

Not sure what you are asking or where you got the result you show. What code are you running? Do you have actual input-output to show? This is what I get.

 

> rlePropRoundTrip [1 .. 40]
True


> repeatedCs [1 .. 40]
"aabbbccccdddddeeeeeefffffffgggggggghhhhhhhhhiiiiiiiiiijjjjjjjjjjjkkkkkkkkkkkklllllllllllllmmmmmmmmmmmmmmnnnnnnnnnnnnnnnoooooooooooooooopppppppppppppppppqqqqqqqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrrsssssssssssssssssssstttttttttttttttttttttuuuuuuuuuuuuuuuuuuuuuuvvvvvvvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzzzzzzzzzzzzzz{{{{{{{{{{{{{{{{{{{{{{{{{{{{|||||||||||||||||||||||||||||}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\DEL\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\128\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\129\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\130\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\131\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\132\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\133\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\134\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\135\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136\136"

 

Last edited by rabbott at 16:23 Mar 10, 2019.
jpatel77
Posts: 44
Posted 16:26 Mar 10, 2019 |
tvo54 wrote:

Ah, there we go. Okey, if i were to run this:

something dwd =  zip ['a' ..] $ map (\n -> n `mod` 100 + 1) dwd, where dwd = [1 up to 40], the output would be:

[('a',2),('b',3),('c',4),('d',5),('e',6),('f',7),('g',8),('h',9),('i',10),('j',11),('k',12),('l',13),('m',14),('n',15),('o',16),('p',17),('q',18),('r',19),('s',20),('t',21),('u',22),('v',23),('w',24),('x',25),('y',26),('z',27),('{',28),('|',29),('}',30),('~',31),('\DEL',32),('\128',33),('\129',34),('\130',35),('\131',36),('\132',37),('\133',38),('\134',39),('\135',40),('\136',41)]
 

And if i choose range up to 40 with Jays code, it would print out:

 

[('A', 2), ('B', 3), ('C', 4), ('D', 5), ('E', 6), ('F', 7), ('G', 8), ('H', 9), ('I', 10), ('J', 11), ('K', 12), ('L', 13), ('M', 14), ('N', 15), ('O', 16), ('P', 17), ('Q', 18), ('R', 19), ('S', 20), ('T', 21), ('U', 22), ('V', 23), ('W', 24), ('X', 25), ('Y', 26), ('Z', 27), ('[', 28), ('\\', 29), (']', 30), ('^', 31), ('_', 32), ('`', 33), ('a', 34), ('b', 35), ('c', 36), ('d', 37), ('e', 38), ('f', 39), ('g', 40), ('h', 41)]

 

Should it matter what is in the first index of the tuples for rlePropRoundTrip to work?

Are you sure you did myRange('a') myCount('a') and not myRange('A') myCount('A')?

myRange('a') myCount('a') generates this seq on my machine:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\x7f', '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87', '\x88']

Which seems almost exactly like what you asked for, except for the later part where I believe it shows hex numbers instead of decimals.

Last edited by jpatel77 at 23:35 Mar 10, 2019.
tvo54
Posts: 7
Posted 16:39 Mar 10, 2019 |

Ah, yeh it worked with myRange('a')!!! I was just confused by the hex. Okey, everything is clear now. Thanks Jay and Dr. Abbott.