Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to print the pattern
for example
if i take the string INDIA
it will print
I I I I I I I I I
I N N N N N N N I
I N D D D D D N I
I N D I I I D N I
I N D I A I D N I
I N D I I I D N I
I N D D D D D N I
I N N N N N N N I
I I I I I I I I I


Thank you in advance

What I have tried:

class pat:
    def __init__(self,str):
        self.a=str
    def pattern(self):
        a=self.a
        b=len(a)
        for i in range(1,b+1):
            c=((10**i)//9)**2
            for j in range(b+1-i,1,-1):
                print (" ",end=" ")
            while c!=0:
                d=int((c%10)-1)
                c=c//10
                print (a[d],end=" ")
            print ()
        for i in range(b-1,0,-1):
            c=((10**i)//9)**2
            for j in range(1,b+1-i):
                print (" ",end=" ")
            while c!=0:
                d=int((c%10)-1)
                c=c//10
                print (a[d],end=" ")
            print ()
ob1=pat(input("ENter string\n"))
ob1.pattern()

it works but prints a diamond.not a square
Posted
Updated 25-Jul-18 10:35am
v4
Comments
Patrice T 25-Jul-18 14:23pm    
I updated your question to show the square.
It is the 'code' button that do the stuff.

So, you show no attempt to solve the problem yourself, you have no question, you just want us to do your HomeWork.
HomeWork problems are simplified versions of the kind of problems you will have to solve in real life, their purpose is learning and practicing.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
 
Share this answer
 
Comments
coderrrrrrrrrr12 25-Jul-18 14:35pm    
i dont want the code.i just want the logic. i have tried some things like making the row constant and printing and making the column constant and printing.but i cant seem to get logic out of this pattern.So any help with the logic will be a lot of help to me.
Patrice T 25-Jul-18 14:52pm    
Take a sheet of paper and a pencil.
Draw the square by replacing the letters with their positions in the word, note the rows and columns numbers.
You should see something, at least in upper left of square.
Patrice T 25-Jul-18 15:00pm    
Start by solving:
I I I I I
I N N N N
I N D D D
I N D I I
I N D I A
coderrrrrrrrrr12 25-Jul-18 15:36pm    
a="INDIA"
b=len(a)
for i in range(0,b):
    for j in range(0,b):
        if i>j:
            print(a[j],end="")
        else:
            print(a[i],end="")
    print()
Patrice T 25-Jul-18 15:52pm    
You have upper left, now compare with upper right, the difference is not complicated.
once it is done, compare upper half with lower half.
It is very simple once you realize you have to choose the letter based on current distance from central letter, where the distance function is
max (|x-L| , |y-L|)
(Where L is the length of the string)

Try
def distance(x,y,l):
  return max( abs(x-l), abs(y-l))

s = "CODEPROJECT"
l = len(s)
for y in range(0,l*2-1):
  for x in range(0,l*2-1):
    d = distance(x,y,l-1)
    print(s[l-d-1], end='')
  print()
 
Share this answer
 
v2
a) Note that the word has n letters
b) Note that the number of rows and columns is n * 2 - 1
c) Note that when the entire word is printed, the index of the letter printed in column c is n - ABS(n - c)
d) Note that for each row r, the index of the letter printed does not exceed n - ABS(n - r)

You should now have enough information to write a solution.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900