I started to think how to create up and down style loop, and what I ended up was this. Wanted to do it little unconventional way though to demonstrate the property of range as list.
for n in range(8): for i in range(n)+range(n-2,-1,-1): print i, print """ Output: 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 4 5 4 3 2 1 0 0 1 2 3 4 5 6 5 4 3 2 1 0 """
Edited 6 Years Ago by pyTony: n/a
Somebody has campaign against me. Please private message me or give at least reputation comment if you down or upvote me.
Here is a more complete use case context of the general version of this principle: ( I left out collecting the result in list of strings and centering it. Here I only print it)
## here more complete example of real world use, see the original case for basic show of principle def yoyo(seq): """yoyo sequences by reflecting the sequence backwards so that the last element becomes the center element of the returned sequence""" return seq+seq[-2::-1] ## forward sequence and backwards (step -1) from second last yoyol=[range(6),['a','b','c'],'-+|*'] for i in yoyol: for j in range(7): ## if j goes over length of sequence, i[:j] is copy of whole sequece i[:] for x in yoyo(i[:j]): print x, print """ Output: 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 4 5 4 3 2 1 0 a a b a a b c b a a b c b a a b c b a a b c b a - - + - - + | + - - + | * | + - - + | * | + - - + | * | + - >>> """
Edited 6 Years Ago by pyTony: Improving language
I am coding for Python 2.6, usually you can easily convert simple code snippets by using 2to3. Usually difference is the print statement without braces only (even I do not know how the print function handles all variations of print statement, I think generally by keyword arguments)
Here is code I produced for Python 3 by using __future__ import in Python 2.6 and document
## effort on Python 3 compatibility from __future__ import print_function def yoyo(seq): """yoyo sequences by reflecting the sequence backwards so that the last element becomes the center element of the returned sequence""" return seq+seq[-2::-1] ## forward sequence and backwards (step -1) from second last yoyol=[range(8),['a','b','c'],'-+|*'] for i in yoyol: for j,_ in enumerate(i): for x in yoyo(i[:j]): print(x,end='') print() """ Output: 0 010 01210 0123210 012343210 01234543210 0123456543210 a aba - -+- -+|+- >>> """
Edited 6 Years Ago by pyTony: Code Python 3
Very nice idea.
Pretty cool idea, i will save this one hoping to come up with a use for it sometimes :D
Main use for me would be to do pattern of colored square with items in the sequence as colors or squares with color where colors form overlapping squares instead of simple diagonal lines as my diagonal stripes function (generelized checkered board routine) which does not look so special with many colors as stripes.
def diag_stripe(top,colors,sqsize,cols,rows=None): """ top=object to draw in, colors=list of color names, sqsize=sqsize of the square cols,rows = number of cols and rows """ if not rows: rows=cols ## can not put rows=cols in parameter list cl=len(colors) col=0 ## diag stripes for any sqsize, any number of colors, rectangle for j in range(0,rows*sqsize,sqsize): for i in range (0,cols*sqsize,sqsize): fill=colors[col] top.create_rectangle(i,j,i+sqsize, j+sqsize,fill=fill) col=(col+1)%cl ## make to start with different color next line, ## if number of colors divides the number of cols if not cols % cl: col=(col+1)%cl
This was the motivation for the sequence style, but I did not come around to improve this pattern maker wiith use of it.
Edited 6 Years Ago by pyTony: n/a
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Here is the sample output that I ...
Hello All ...
Iam Getting An Error With try to excecute the stored procedure .
I have Have Sql database , the stored procedure like so :
USE [MPRS] GO /****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE ...
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...