Home > Blockchain >  Turn text around a certain character in python
Turn text around a certain character in python

Time:12-09

For school, I need to code a program that sort of turns text around a given character and deletes that character. The outcome will be sort of simple ASCII art. You need to input the character where the text will be turned around, and the amount of lines that the given input will be. An example would be: Input:

|
26
                   |                          oooo$$$$$$$$$$$$oooo          
      |                      oo$$$$$$$$$$$$$$$$$$$$$$$$o                    
$o         o$   $$ o$    |                   oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$o       $$ $$ $$o$   |   o $ oo        o$$$$$$$$$$$$$$$$$$$$
    $$$o$$o$    |oo $ $ "$      o$$$$$$$$$    $$$$$$$$$$$$$    $$$$$$$$$o   
$$$$$$$o    $$$$$$$$     |"$$$$$$o$     o$$$$$$$$$      $$$$$$$$$$$      $$$
$$     |  $$$$$$$    $$$$$$$$$$$      $$$$$$$$$$$      $$$$$$$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$  """$$$       |  $$$$$$$$$$$$$$$$$$$$$$$    $$$$$$$$$$$$$  
$$$$$$$$$     "$$$      |   "$$$""""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$     "$$$o    |    $$$   o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$o   |   o$$"   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$       $
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$ooooo$$$$o |   $$$    $$$$$$$$$$$$$$$$
$$$oooo$$$$$  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$   o$$$$$$$$$$$$$$$$$|  o
$$$$$$$$$$$$$$$$$$     $$$$""""""""      |  $$$$$$$$"$$$$   $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$"      o$$$               | """"       $$$$    "$$
         "$$$o     """$$$$$$$$$$$$$$$$$$"$$"         $$$                |   
          "$$""$$$$$$""""           o$$$                 |              $$$o
            o$$$"                  |               $$$$o                 oo 
                |                "$$$$o      o$$$$$$o"$$$$o        o$$$$    
|                  "$$$$$oo     ""$$$$o$$$$$o   o$$$$""                     
o$$$$$$$$$"""                        |                     ""$$$$$oooo  "$$$
             ""$$$$$$$oo $$$$$$$$$$                             |           
$$$$$$$$$$$                            |                                """"
$$$$$$$$                           |                                    $$$$
                           |                                     $$$$$$$$$$"
                                 "$$$""""                             | 

output:

                          oooo$$$$$$$$$$$$oooo                             
                      oo$$$$$$$$$$$$$$$$$$$$$$$$o                          
                   oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o         o$   $$ o$    
   o $ oo        o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o       $$ $$ $$o$   
oo $ $ "$      o$$$$$$$$$    $$$$$$$$$$$$$    $$$$$$$$$o       $$$o$$o$    
"$$$$$$o$     o$$$$$$$$$      $$$$$$$$$$$      $$$$$$$$$$o    $$$$$$$$     
  $$$$$$$    $$$$$$$$$$$      $$$$$$$$$$$      $$$$$$$$$$$$$$$$$$$$$$$     
  $$$$$$$$$$$$$$$$$$$$$$$    $$$$$$$$$$$$$    $$$$$$$$$$$$$$  """$$$       
   "$$$""""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     "$$$      
    $$$   o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     "$$$o    
   o$$"   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$       $$$o   
   $$$    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$ooooo$$$$o 
  o$$$oooo$$$$$  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$   o$$$$$$$$$$$$$$$$$
  $$$$$$$$"$$$$   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     $$$$""""""""      
 """"       $$$$    "$$$$$$$$$$$$$$$$$$$$$$$$$$$$"      o$$$               
            "$$$o     """$$$$$$$$$$$$$$$$$$"$$"         $$$                
              $$$o          "$$""$$$$$$""""           o$$$                 
               $$$$o                 oo             o$$$"                  
                "$$$$o      o$$$$$$o"$$$$o        o$$$$                    
                  "$$$$$oo     ""$$$$o$$$$$o   o$$$$""                     
                     ""$$$$$oooo  "$$$o$$$$$$$$$"""                        
                        ""$$$$$$$oo $$$$$$$$$$                             
                                """"$$$$$$$$$$$                            
                                    $$$$$$$$$$$$                           
                                     $$$$$$$$$$"                           
                                      "$$$""""    

I assume I would have to use substrings in order to do this, but I'm not sure how exactly I would have to do this. (sorry for bad explanation/english)

CodePudding user response:

This is not too difficult. It helps to start with a plan of how you want to attack it.

  1. Start by getting the delimiter character and length using input and create a variable to store the result.
  2. Create a for loop that runs for length iterations.
  3. Get each line by calling input and split each line by the delimiter
  4. Reverse the start and end, and then add it to the result.

You could have something like this:

delimiter = input()
length = int(input())
result = ""

for i in range(length):
    line = input()
    [end, start] = line.split(delimiter)
    result  = start   end   "\n"

print(result)

CodePudding user response:

Once you have the separator and lines in separate variables, you can use split to get the inverted left and right sides and print them in the correct order:

s = '''|
26
                   |                          oooo$$$$$$$$$$$$oooo          
      |                      oo$$$$$$$$$$$$$$$$$$$$$$$$o                    
$o         o$   $$ o$    |                   oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$o       $$ $$ $$o$   |   o $ oo        o$$$$$$$$$$$$$$$$$$$$
    $$$o$$o$    |oo $ $ "$      o$$$$$$$$$    $$$$$$$$$$$$$    $$$$$$$$$o   
$$$$$$$o    $$$$$$$$     |"$$$$$$o$     o$$$$$$$$$      $$$$$$$$$$$      $$$
$$     |  $$$$$$$    $$$$$$$$$$$      $$$$$$$$$$$      $$$$$$$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$  """$$$       |  $$$$$$$$$$$$$$$$$$$$$$$    $$$$$$$$$$$$$  
$$$$$$$$$     "$$$      |   "$$$""""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$     "$$$o    |    $$$   o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$o   |   o$$"   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$       $
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$ooooo$$$$o |   $$$    $$$$$$$$$$$$$$$$
$$$oooo$$$$$  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$   o$$$$$$$$$$$$$$$$$|  o
$$$$$$$$$$$$$$$$$$     $$$$""""""""      |  $$$$$$$$"$$$$   $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$"      o$$$               | """"       $$$$    "$$
         "$$$o     """$$$$$$$$$$$$$$$$$$"$$"         $$$                |   
          "$$""$$$$$$""""           o$$$                 |              $$$o
            o$$$"                  |               $$$$o                 oo 
                |                "$$$$o      o$$$$$$o"$$$$o        o$$$$    
|                  "$$$$$oo     ""$$$$o$$$$$o   o$$$$""                     
o$$$$$$$$$"""                        |                     ""$$$$$oooo  "$$$
             ""$$$$$$$oo $$$$$$$$$$                             |           
$$$$$$$$$$$                            |                                """"
$$$$$$$$                           |                                    $$$$
                           |                                     $$$$$$$$$$"
                                 "$$$""""                             |     '''
separator,count,*lines = s.split("\n")
for line in lines:
    right,left = line.split(separator)
    print(left right)

output:

                          oooo$$$$$$$$$$$$oooo                             
                      oo$$$$$$$$$$$$$$$$$$$$$$$$o                          
                   oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o         o$   $$ o$    
   o $ oo        o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o       $$ $$ $$o$   
oo $ $ "$      o$$$$$$$$$    $$$$$$$$$$$$$    $$$$$$$$$o       $$$o$$o$    
"$$$$$$o$     o$$$$$$$$$      $$$$$$$$$$$      $$$$$$$$$$o    $$$$$$$$     
  $$$$$$$    $$$$$$$$$$$      $$$$$$$$$$$      $$$$$$$$$$$$$$$$$$$$$$$     
  $$$$$$$$$$$$$$$$$$$$$$$    $$$$$$$$$$$$$    $$$$$$$$$$$$$$  """$$$       
   "$$$""""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     "$$$      
    $$$   o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     "$$$o    
   o$$"   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$       $$$o   
   $$$    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$ooooo$$$$o 
  o$$$oooo$$$$$  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$   o$$$$$$$$$$$$$$$$$
  $$$$$$$$"$$$$   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$     $$$$""""""""      
 """"       $$$$    "$$$$$$$$$$$$$$$$$$$$$$$$$$$$"      o$$$               
            "$$$o     """$$$$$$$$$$$$$$$$$$"$$"         $$$                
              $$$o          "$$""$$$$$$""""           o$$$                 
               $$$$o                 oo             o$$$"                  
                "$$$$o      o$$$$$$o"$$$$o        o$$$$                    
                  "$$$$$oo     ""$$$$o$$$$$o   o$$$$""                     
                     ""$$$$$oooo  "$$$o$$$$$$$$$"""                        
                        ""$$$$$$$oo $$$$$$$$$$                             
                                """"$$$$$$$$$$$                            
                                    $$$$$$$$$$$$                           
                                     $$$$$$$$$$"                           
                                      "$$$""""       
  • Related