| | |
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:
Solved Threads: 0
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
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.
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.
Here is how we could do in pseudo-algorithmic language. Let's call the elements of FIVEHIGH "ideograms"
Now, you write the python code for this strategy !
Python Syntax (Toggle Plain Text)
create an empty list L for each character c in word: find the ideogram x corresponding to c append x to our list L # now the problem is to display the result # we display the 5 lines one after the other for each i in 1, 2, 3, 4, 5: initialize the i-th line S to the empty string for each ideogram x in L: find the i-th line y of x # for example 2nd line of the first ideogram is 0 X 0 X concatenate y to S print S
Now, you write the python code for this strategy !
Last edited by Gribouillis; Sep 3rd, 2009 at 2:39 pm. Reason: indentation
•
•
Join Date: Sep 2009
Posts: 16
Reputation:
Solved Threads: 0
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.
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?
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.
•
•
Join Date: Dec 2006
Posts: 1,054
Reputation:
Solved Threads: 297
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)
base_dict = {} base_dict["A"] = """X 0 X X 0 X 0 X 0 0 0 X 0 X 0 X 0 X 0 X""" base_dict["B"] = """0 0 X X 0 X 0 X 0 0 X X 0 X 0 X 0 0 X X""" base_dict["C"] = """X 0 X X 0 X 0 X 0 X X X 0 X 0 X X 0 X X """ for key in ("A", "B", "C"): print key, "-" *60 print base_dict[key]
Last edited by woooee; Sep 3rd, 2009 at 4:47 pm.
Linux counter #99383
•
•
Join Date: Sep 2009
Posts: 16
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
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
Ok, here is a solution
python Syntax (Toggle Plain Text)
base = "ABC" FIVEHIGH = ( """ X 0 X X 0 X 0 X 0 0 0 X 0 X 0 X 0 X 0 X """, """ 0 0 X X 0 X 0 X 0 0 X X 0 X 0 X 0 0 X X """, """ X 0 X X 0 X 0 X 0 X X X 0 X 0 X X 0 X X """) class Ideogram(tuple): def __new__(cls, string): return tuple.__new__(cls, (y for y in (x.strip() for x in string.split("\n")) if y)) def __init__(self, *args): assert(len(self) == 5) base_dict = dict(zip(tuple(base), (Ideogram(x) for x in FIVEHIGH))) def translate(word): lines = list(list() for i in range(5)) for letter in (c for c in word.upper() if c in base_dict): # skip characters that dont belong to base for i, x in enumerate(base_dict[letter]): lines[i].append(x) return "\n".join(" ".join(L) for L in lines) if __name__ == "__main__": word = raw_input("Enter word:") print translate(word)
•
•
Join Date: Dec 2006
Posts: 1,054
Reputation:
Solved Threads: 297
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)
def join_letters( letter_list, base_dict ): combined_rows_dict = {} ##--- base length to test for equality len_letter = len(base_dict[letter_list[0]]) for j in range(0, len(letter_list)): this_letter = letter_list[j] ##--- all letters must be in the dictionary if this_letter not in base_dict: print "letter %s not in base_dict" % (this_letter) return "***" ##--- test for all translations are equal in length if len(base_dict[this_letter]) != len_letter: print "unequal letter lengths", len_letter, len(base_dict[this_letter]) return "***" ##--- convert triple quote string into rows in a list string_list = base_dict[this_letter].split("\n") print "string_list", string_list ##--- append each row to the corresponding dictionary key for row in range(0, len(string_list)): if row not in combined_rows_dict: combined_rows_dict[row] = [] combined_rows_dict[row].append(string_list[row]) for row in range(0, len(combined_rows_dict)): this_row = combined_rows_dict[row] print " ".join(this_row) return "Success" base_dict = {} base_dict["A"] = """X 0 X X 0 X 0 X 0 0 0 X 0 X 0 X 0 X 0 X""" base_dict["B"] = """0 0 X X 0 X 0 X 0 0 X X 0 X 0 X 0 0 X X""" base_dict["C"] = """X 0 X X 0 X 0 X 0 X X X 0 X 0 X X 0 X X""" ##for key in ("A", "B", "C"): ## print key, "-" *60 ## print base_dict[key] print join_letters(["A", "C", "B"], base_dict)
Last edited by woooee; Sep 3rd, 2009 at 6:42 pm.
Linux counter #99383
![]() |
Similar Threads
- Starting Python (Python)
- Lisp load function in windows (Computer Science)
- Creating a Basic String Database (C++)
- Replacement Site - Old Links? (Site Layout and Usability)
- Cryptography In C (C)
- Odd string concatenation behavior? (Perl)
- syntax errors and the check module command? (Python)
- Problem with Perl syntax highlighter (DaniWeb Community Feedback)
- regular expressions (Java)
- Desiging a set of rules for a match (C++)
Other Threads in the Python Forum
- Previous Thread: Read between 2 keywords in file
- Next Thread: building pymedia
| Thread Tools | Search this Thread |
Tag cloud for Python
address anydbm app bash beginner bits changecolor cipher class clear code conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format ftp function gui handling homework images import info input java keycontrol line linux list lists loan loop matching microcontroller mouse number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer pyqt python random rational raw_input recursion recursive scrolledtext searchingfile shebang singleton split ssh string strings table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode unix url urllib urllib2 valueerror variable windows word wx.wizard wxpython xlwt






