Make programm, that asks from user his name and number of lines for the incoming shape.
For example.
Peeter and 6, the example wil be :
Peeter shape:
* * * * * *
* *
* *
* *
* *
* * * * * *
So my code is:
def square():
row = int(input('Insert number of rows'))
line = len(input('Insert your name'))
for r in range(1, (row+1), 1):
if r == 1 or r == rida:
for v in range(1, (line+1), 1):
print('*', end=' ')
else:
print('*', end=' ')
for v in range(2, row, 1):
print(' ', end=' ')
print('*', end=' ')
print()
square()
But the problem is, it gives me syntaxerror and I am out of ideas how to fix it.
Thank you for your help and time for advance.
Nika
Related
I have a very complex integral to calculate:
from __future__ import division
from scipy.integrate import quad, nquad
import numpy as np
alpha = np.array([0.298073, 1.242567, 5.782948, 38.474970])
trial = np.array([0.08704173, 0.52509737, 0.51920929, 0.31233737])
class EigenvalueProblem:
def __init__(self, a, t):
self.alpha = a
self.trial = t
# Hamiltonian, interaction part
def hartree_integrand(self, coeff):
def hartree_potential(rr2):
return np.array([coeff[ii] * coeff[jj] *
np.exp(-(self.alpha[ii] +
self.alpha[jj]) * rr2 ** 2)
for ii in range(0, 4) for jj in range(0, 4)]).sum()
def length(theta, rr1, rr2):
return 1 / np.sqrt(rr1 ** 2 + rr2 ** 2 -
2 * rr1 * rr2 * np.cos(theta))
def tmp(theta, rr1, rr2):
return 8 * np.pi ** 2 * rr1 ** 2 * rr2 ** 2 * \
np.sin(theta) * hartree_potential(rr2) * \
length(theta, rr1, rr2)
def integrand(ii, jj, theta, rr1, rr2):
return np.exp(-(self.alpha[ii] + self.alpha[jj]) * rr1 ** 2) * tmp(theta, rr1, rr2)
return [
nquad(lambda theta, rr1, rr2: integrand(i, j, theta, rr1, rr2),
[[0, np.pi], [0, np.inf], [0, np.inf]]) for i in range(0, 4) for j in range(0, 4)]
hat = EigenvalueProblem(alpha, trial)
print hat.hartree_integrand(trial)
mathematically what I want to calculate is like this (which is the integrand function), with paremeters here. However, it takes more than several hours to compute this integral. I wonder is there any method to speed up it? Thank you very much!
You ought first to extend limits in integration over r1 and r2 to be from -Infinity to +Infinity - extend limits, multiply by 1/2*1/2, etc
Second, switch to use Gauss-Hermite quadrature, which is exactly suited to integrate function with e-x2 kernels.
Appropriate code is in NumPy, see references therein
I want to print triangle & pyramid "*" using for loop.
can somebody help me on this?
Output like :
*****
****
***
**
*
and
*
**
***
****
*****
1: Print triangle using While loop
n = 5
while n >= 1
puts "* " * n
n = n - 1
end
* * * * *
* * * *
* * *
* *
*
n = 1
while n <= 5
puts ("* " * n).rjust(10)
n += 1
end
*
* *
* * *
* * * *
* * * * *
2: Print pyramid using loop
n = 4 # Set number of rows
i = 1
1.upto(n) do
print ' ' * n
print '*' * (2 * i - 1)
print "\n"
n -= 1
i += 1
end
*
***
*****
*******
5.downto(1).each{|n| puts ("*" * n).ljust(5)}
1.upto(5).each{|n| puts ("*" * n).rjust(5)}
I have no coding experience other than this book
Programming Swift! Swift 2 Kindle Edition
by Nick Smith (Author)
I am currently at Chapter
5.3 Nested FOR LOOPS
// NESTED FOR LOOP #2
This code -
for var a = 0; a < 11; a++ {
print("")
for var b = 0; b < a; b++ {
print("*", terminator: " ")
}
}
GENERATES THIS PATTERN...
Now [after several/ 4 hours 'odd'] I SIMPLY CAN'T WORK OUT HOW TO CHANGE THE ABOVE 'simple' [if you know how] CODE TO GENERATE THIS PATTERN??
I (think I) can see Outer and Inner loops I just can't work out the rest!?? I have tried every variation I can think of!?? (and am aware that just doing 'permutations' doesn't mean I have true understanding of what I am trying to do!...)
Tried using --operators and changing [most/ all] values [but 'permutations' is a limited method]
I feel like a total fool but figure if it's the very first time I've seen this stuff maybe it's not so bad, these things take learning!??
Help (the answer LOL) would be GREATLY appreciated π¬ π¬ π¬
for var a = 10; a > 0; a-- {
for var b = 0; b < a; b++ {
print("*", terminator: " ")
}
print()
}
prints
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
UPDATE for nowadays Swift syntax, with the same functionality
for a in stride(from: 10, through: 0, by: -1) {
for _ in stride(from: 0, to: a, by: 1) {
print("*", terminator: " ")
}
print()
}
How to do this systematically: If you want to got for example user3441734's output: There are 11 lines. We number the lines from 0 to 10. So we have a loop that sets line to the values 0 to 10.
for var line = 0; line < 11; ++line
Next, what do we want to print in each line? In line 0 we want to print 11 * characters. In line 10 we want to print 1 star character. The number of stars is 11 - line. How do I get the expression 11 - line? The number of stars goes down as line goes up, so it must be something - line. When line = 0 there must be 11 stars, so something - 0 = 11, and something = 11. So the first line in the loop:
let starcount = 11 - line
Then we want to print (star count) times a star and a space character, follow by starting a new line.
for var star = 0; star < starcount; ++star {
print ("*", terminator: " ")
}
print ("")
All together:
for var line = 0; line < 11; ++line {
let starcount = 11 - line
for var star = 0; start < star count; ++star {
print ("*", terminator: " ")
}
print ("")
}
And we simplify the loops a bit:
for var line in 0 ..< 11 {
let starcount = 11 - line
for var star in 0 ..< starcount {
print ("*", terminator: " ")
}
print ("")
}
If you wanted a different pattern, all you have to do is change the number 11 if the number of lines is different, and change the calculation of starcount. Actually it would be better to have a variable for linecount as well, so changing for a different pattern is even easier:
let linecount = 11
for var line in 0 ..< line count {
let starcount = linecount - line
for var star in 0 ..< starcount {
print ("*", terminator: " ")
}
print ("")
}
I have written an application which will write square with diagonal (from left side) - output:
+ * * * *
* + * * *
* * + * *
* * * + *
* * * * +
Code for first application:
PROGRAM cycle4;
USES CRT;
VAR a,r,s:INTEGER;
BEGIN
CLRSCR;
WRITE (βEnter the number of lines :β) ;
READLN(a);
FOR r:= 1 TO a DO
BEGIN
FOR s:=1 TO a DO
IF r = s THEN WRITE(β+β)
ELSE WRITE(β*β) ;
WRITELN;
END;
READLN;
END.
And now I have to create an application which will write square with diagonal (from right side) - output:
* * * * +
* * * + *
* * + * *
* + * * *
+ * * * *
But I don't know how can I write it. Can you help me?
Thanks :)
The line of code which defines the position of + sign is that:
IF r = s THEN WRITE(β+β)
and this is the only line you need to change:
IF r + s = a + 1 THEN WRITE(β+β)
I think this should work, check with Pascal compiler, haven't used it for about 10 years :)
I am using Ruby 1.9.3. I have done pattern program like follow:
n = 1
while n <= 5
n.downto 1 do |i|
print "* "
end
puts
n += 1
end
Output of above program is like follow:
*
* *
* * *
* * * *
* * * * *
Now I am trying to do pattern program like follow:
*
* *
* * *
* * * *
* * * * *
I am not getting idea how can I do it?
Could anyone help me on this?
Thank you.
You can use rjust:
n = 1
while n <= 5
puts "* " * n
n += 1
end
*
* *
* * *
* * * *
* * * * *
n = 1
while n <= 5
puts ("* " * n).rjust(10)
n += 1
end
*
* *
* * *
* * * *
* * * * *
A shortened version of this will be:
5.times { |i| puts ('* ' * (i+1)) }
and
5.times { |i| puts ('* ' * (i+1)).rjust(10) }
You can do:
1.upto 5 do |n|
print ' ' * (5-n)
print '* ' * n
puts
end
Here's another way:
def print_two_ways(n, spaces=0)
arr = Array.new(n) { |i| Array.new(n) { |j| (i >= j) ? '*' : ' ' } }
print_matrix(arr, spaces)
puts
print_matrix(arr.map(&:reverse), spaces)
end
def print_matrix(arr, spaces = 0)
sep = ' '*(spaces)
arr.each { |r| puts "#{r.join(sep)}" }
end
print_two_ways(5)
*
**
***
****
*****
*
**
***
****
*****
print_two_ways(5,1)
*
* *
* * *
* * * *
* * * * *
*
* *
* * *
* * * *
* * * * *
a = 5
b = 1
while a>0
while b<=5
puts "*"*b
b = b+1
a = a-1
end
end
*
**
***
****
*****
Here, a is 5 and b is 1.
In the first iteration of the outer while loop, a is 1 and the inner while loop is inside the body of the outer while loop. So, the inner while loop will be executed and "*"1 ( b is 1 ) i.e "" will be printed and b will become 2 and a will become 4.
Here is simple set variable and use all programs.
# Set vars
n = 4 # Set number of rows
br = "\n" * 2
# Simple loop
puts "Right triangle:#{br}"
for i in 1..n do
puts "* " * i
end
puts br
=begin
simple loop result:
*
* *
* * *
* * * *
=end
# Countdown loop
puts "Inverted right triangle:#{br}"
n.downto(0) do
puts "* " * n
n -= 1
end
puts br
=begin
countdown loop result:
* * * *
* * *
* *
*
=end
# Function loop
puts "Inverted pyramid:#{br}"
n = 4 # Reset number of rows
for i in 1..n do
# Use a func to reduce repetition
def printer(var, str)
print "#{str}" * (2 * var - 1)
end
printer(i, " ")
printer(n, "* ")
print "\n"
n -= 1
end
puts br
=begin
function loop result:
* * * * * * *
* * * * *
* * *
*
=end
# Count up loop
puts "Close pyramid:#{br}"
n = 4 # Set number of rows
i = 1
1.upto(n) do
#n.times do
# print ' '
#end
print ' ' * n
#(2 * i - 1).times do
# print '*'
#end
print '*' * (2 * i -1)
print "\n"
n -= 1
i += 1
end
print br
=begin
count up loop result:
*
***
*****
*******
=end
I'm still new to Ruby, but this was my solution. Anything wrong with doing it this way?
def staircase(n)
sum_tot = n
n.times do
sum_tot-= 1
space = n - sum_tot
puts ('#' * space).rjust(n)
end
end