Creating a replacement cypher with triple-quoted strings

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #11
Sep 3rd, 2009
Wait, I shouldn't use triple quoted strings as the desired output?

That's what my original question was asking:

How can I get an input of 'cab' to
X 0 X X X 0 X X 0 0 X X
0 X 0 X 0 X 0 X 0 X 0 X
0 X X X 0 0 0 X 0 0 X X
0 X 0 X 0 X 0 X 0 X 0 X
X 0 X X 0 X 0 X 0 0 X X
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 965
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #12
Sep 3rd, 2009
First, make a list with the strings which correspond to CAB, then think about how to produce your output with this list.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #13
Sep 3rd, 2009
The only way I could come up with is to have the program print four letters (one row), and then start a new line. Print the next four, start a new line, etc.

The problem with that is they wouldn't form next to each other like a word, they would be stacked like the for c in word: print(c) did, wouldn't it?

To fix that, I would have to "scrape off" the top layer of each word, print it as a concatenation, then strip out the next line, print it, and so forth.

Is it possible to pull only part of a string, or would I have to make a list with five lines for each letter?

Well, even if I did that, how would I tell it where to find it's information? I don't think there's a fairly simple way to have it read 1.1, then skip to 2.1 five strings away, then come back to 1.2.

I'm afraid I have no idea where you're trying to lead me, Gribouillis.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 965
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #14
Sep 3rd, 2009
Here is how we could do in pseudo-algorithmic language. Let's call the elements of FIVEHIGH "ideograms"

  1. create an empty list L
  2. for each character c in word:
  3. find the ideogram x corresponding to c
  4. append x to our list L
  5. # now the problem is to display the result
  6. # we display the 5 lines one after the other
  7. for each i in 1, 2, 3, 4, 5:
  8. initialize the i-th line S to the empty string
  9. for each ideogram x in L:
  10. find the i-th line y of x # for example 2nd line of the first ideogram is 0 X 0 X
  11. concatenate y to S
  12. print S

Now, you write the python code for this strategy !
Last edited by Gribouillis; Sep 3rd, 2009 at 2:39 pm. Reason: indentation
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #15
Sep 3rd, 2009
This may take me a bit, but I'll get on it!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #16
Sep 3rd, 2009
I'm afraid I'm going to have to throw my hands up on this one. My brain is leaking out of my ears.

I get what we're trying to do, but I don't know how to tell the computer itself. I'm brand-spanking new to Python and can't find what I need in the documentation.

find the ideogram x corresponding to c / append x to our list L
I know what this means, but I can't for the life of me figure out how to do it. I'm trying to read Greek and speak Russian!

The biggest problem I'm having with it is that I can't figure out how to get the system to read them as a group of letters, rather than X's and 0's.

Wait, would I want to use a list and slice it? But then how would it know which position is correct?
Last edited by Warkthogus; Sep 3rd, 2009 at 4:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,054
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 297
woooee woooee is offline Offline
Veteran Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #17
Sep 3rd, 2009
I for one am not sure what you are trying to do. It is something like this? If not, please post some input and desired output examples. Also, if joining, remember that each triple quoted string is one string and not a list.
  1. base_dict = {}
  2. base_dict["A"] = """X 0 X X
  3. 0 X 0 X
  4. 0 0 0 X
  5. 0 X 0 X
  6. 0 X 0 X"""
  7.  
  8. base_dict["B"] = """0 0 X X
  9. 0 X 0 X
  10. 0 0 X X
  11. 0 X 0 X
  12. 0 0 X X"""
  13.  
  14. base_dict["C"] = """X 0 X X
  15. 0 X 0 X
  16. 0 X X X
  17. 0 X 0 X
  18. X 0 X X
  19. """
  20.  
  21. for key in ("A", "B", "C"):
  22. print key, "-" *60
  23. print base_dict[key]
Last edited by woooee; Sep 3rd, 2009 at 4:47 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 16
Reputation: Warkthogus is an unknown quantity at this point 
Solved Threads: 0
Warkthogus Warkthogus is offline Offline
Newbie Poster

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #18
Sep 3rd, 2009
Woooee, I'm not sure how else to explain it. I laid out my thought process earlier, and don't know any other way to show it.

How can I get an input of 'cab' to
X 0 X X X 0 X X 0 0 X X
0 X 0 X 0 X 0 X 0 X 0 X
0 X X X 0 0 0 X 0 0 X X
0 X 0 X 0 X 0 X 0 X 0 X
X 0 X X 0 X 0 X 0 0 X X
I want the letters to be converted into the ascii "art" of letters. I'm trying to make a program to output a blueprint for making letters with cups in chain link fences.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 965
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Creating a replacement cypher with triple-quoted strings

 
0
  #19
Sep 3rd, 2009
Ok, here is a solution
  1. base = "ABC"
  2.  
  3. FIVEHIGH = (
  4. """
  5. X 0 X X
  6. 0 X 0 X
  7. 0 0 0 X
  8. 0 X 0 X
  9. 0 X 0 X
  10. """,
  11. """
  12. 0 0 X X
  13. 0 X 0 X
  14. 0 0 X X
  15. 0 X 0 X
  16. 0 0 X X
  17. """,
  18. """
  19. X 0 X X
  20. 0 X 0 X
  21. 0 X X X
  22. 0 X 0 X
  23. X 0 X X
  24. """)
  25.  
  26. class Ideogram(tuple):
  27. def __new__(cls, string):
  28. return tuple.__new__(cls, (y for y in (x.strip() for x in string.split("\n")) if y))
  29. def __init__(self, *args):
  30. assert(len(self) == 5)
  31.  
  32.  
  33. base_dict = dict(zip(tuple(base), (Ideogram(x) for x in FIVEHIGH)))
  34.  
  35. def translate(word):
  36. lines = list(list() for i in range(5))
  37. for letter in (c for c in word.upper() if c in base_dict): # skip characters that dont belong to base
  38. for i, x in enumerate(base_dict[letter]):
  39. lines[i].append(x)
  40. return "\n".join(" ".join(L) for L in lines)
  41.  
  42. if __name__ == "__main__":
  43. word = raw_input("Enter word:")
  44. print translate(word)
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,054
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 297
woooee woooee is offline Offline
Veteran Poster

Re: Creating a replacement cypher with triple-quoted strings

 
1
  #20
Sep 3rd, 2009
My attempt. Once again, if not correct, post back with more info. The trick (I think) is to split each string on the "\n" to create individual rows, and is similiar to Gribouillis' solution but doesn't use list comprehension (which is an amazing tool). It instead uses a dictionary with row number as the key, in this case 0 through 4, and appends each row to the corresponding key. Finally, the joined rows are spaced and printed. Hopefully, using a dictionary with the row number is easy to understand.
  1. def join_letters( letter_list, base_dict ):
  2. combined_rows_dict = {}
  3.  
  4. ##--- base length to test for equality
  5. len_letter = len(base_dict[letter_list[0]])
  6.  
  7. for j in range(0, len(letter_list)):
  8. this_letter = letter_list[j]
  9.  
  10. ##--- all letters must be in the dictionary
  11. if this_letter not in base_dict:
  12. print "letter %s not in base_dict" % (this_letter)
  13. return "***"
  14.  
  15. ##--- test for all translations are equal in length
  16. if len(base_dict[this_letter]) != len_letter:
  17. print "unequal letter lengths", len_letter, len(base_dict[this_letter])
  18. return "***"
  19.  
  20. ##--- convert triple quote string into rows in a list
  21. string_list = base_dict[this_letter].split("\n")
  22. print "string_list", string_list
  23.  
  24. ##--- append each row to the corresponding dictionary key
  25. for row in range(0, len(string_list)):
  26. if row not in combined_rows_dict:
  27. combined_rows_dict[row] = []
  28. combined_rows_dict[row].append(string_list[row])
  29.  
  30. print
  31. for row in range(0, len(combined_rows_dict)):
  32. this_row = combined_rows_dict[row]
  33. print " ".join(this_row)
  34. return "Success"
  35.  
  36. base_dict = {}
  37. base_dict["A"] = """X 0 X X
  38. 0 X 0 X
  39. 0 0 0 X
  40. 0 X 0 X
  41. 0 X 0 X"""
  42.  
  43. base_dict["B"] = """0 0 X X
  44. 0 X 0 X
  45. 0 0 X X
  46. 0 X 0 X
  47. 0 0 X X"""
  48.  
  49. base_dict["C"] = """X 0 X X
  50. 0 X 0 X
  51. 0 X X X
  52. 0 X 0 X
  53. X 0 X X"""
  54.  
  55. ##for key in ("A", "B", "C"):
  56. ## print key, "-" *60
  57. ## print base_dict[key]
  58.  
  59. print join_letters(["A", "C", "B"], base_dict)
Last edited by woooee; Sep 3rd, 2009 at 6:42 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC