943,782 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1858
  • Python RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Warkthogus is offline Offline
16 posts
since Sep 2009
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

First, make a list with the strings which correspond to CAB, then think about how to produce your output with this list.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Warkthogus is offline Offline
16 posts
since Sep 2009
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

Here is how we could do in pseudo-algorithmic language. Let's call the elements of FIVEHIGH "ideograms"

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

This may take me a bit, but I'll get on it!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Warkthogus is offline Offline
16 posts
since Sep 2009
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Warkthogus is offline Offline
16 posts
since Sep 2009
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

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.

Quote ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Warkthogus is offline Offline
16 posts
since Sep 2009
Sep 3rd, 2009
0

Re: Creating a replacement cypher with triple-quoted strings

Ok, here is a solution
python Syntax (Toggle Plain Text)
  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)
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Sep 3rd, 2009
1

Re: Creating a replacement cypher with triple-quoted strings

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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Read between 2 keywords in file
Next Thread in Python Forum Timeline: building pymedia





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC