473,769 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with loop of upper & lower letters

Thekid
145 New Member
I had made a post about making a loop using letters instead of numbers and dshimer gave me this solution:
Expand|Select|Wrap|Line Numbers
  1. for i in range(65,70):
  2.     for j in range(65,70):
  3.         for k in range(65,70):
  4.             print chr(i),chr(j),chr(k)
  5.  
which used an example I had posted using numbers. That code works (thanks dshimer) but after toying with it I see that I am going about it in a method that takes a long time to run through and I also need uppers in the loop. I need a loop that will print out like this:
aaaaa
aaaaA
aaaAa
aaAaa
ans so on through every combination. I now have this portion which seems closer but still not quite there:
Expand|Select|Wrap|Line Numbers
  1. from string import letters
  2. #my 'lowers' list has extra chars at the end so I have to trim it
  3. lowers = letters[26:52]
  4. uppers = letters[:26]
  5. for lower in lowers:
  6.     for upper in uppers:
  7.         print lower,upper
  8.  
a A
a B
a C
a D

So my question is how can I get this to run through with 5 values and through every combination, without doing something like this:
Expand|Select|Wrap|Line Numbers
  1. for lower in lowers:
  2.     for upper in uppers:
  3.         print lower,lower,lower,lower,upper
  4.         print lower,lower,lower,upper,lower
  5.  
May 22 '09 #1
2 2883
bvdet
2,851 Recognized Expert Moderator Specialist
Try this:
Expand|Select|Wrap|Line Numbers
  1. def permute5(a):
  2.     b = [(v,w,x,y,z) for v in a for w in a for x in a for y in a for z in a]
  3.     c = ["".join(z) for z in b]
  4.     return b,c
  5.  
  6. b, c = permute5('abcABC')
  7.  
  8. print "\n".join(c)
Partial output:
>>> aaaaa
aaaab
aaaac
aaaaA
aaaaB
aaaaC
aaaba
aaabb
aaabc
aaabA
aaabB
aaabC
aaaca
aaacb
aaacc
aaacA
aaacB
aaacC
aaaAa
............... ...
CCCcC
CCCAa
CCCAb
CCCAc
CCCAA
CCCAB
CCCAC
CCCBa
CCCBb
CCCBc
CCCBA
CCCBB
CCCBC
CCCCa
CCCCb
CCCCc
CCCCA
CCCCB
CCCCC
>>>
May 22 '09 #2
Thekid
145 New Member
Wow....that's great! Thanks!
May 22 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
1986
by: pkilambi | last post by:
I wrote this function which does the following: after readling lines from file.It splits and finds the word occurences through a hash table...for some reason this is quite slow..can some one help me make it faster... f = open(filename) lines = f.readlines() def create_words(lines): cnt = 0 spl_set = '+' for content in lines:
8
3672
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in that it copies one block of data to another block of data until the block that is being copied contains a zero/null. the difference with this code is that it's doing 4bits at a time (all the values are 4bits) and the two blocks of data may not be...
11
1527
by: Steve Clay | last post by:
I have a small C program for a college course. It is meant to encrypt and decrypt lower case letters and leave spaces as spaces. I can't get it to run properly as I think I have a problem in the While construct area. It is driving me mad trying to sort it out - can someone please help. The program is: /* * Program to encrypt a message using the Turbo C random
1
3721
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
1
1634
by: vv1 | last post by:
Write a C program for reading in a message string (with no blanks) and decoding the message. Store the decoded message in another string called outString. After decoding is complete, print the decoded message. Use the following key for decoding.Convert any numeric characters in input (0-9) to a space in the output. Upper case letters in input/message string are decoded by adding 19 to their ASCII/Integer value. Lower...
0
1418
by: vp1 | last post by:
Write a C program for reading in a message string (with no blanks) and decoding the message. Store the decoded message in another string called outString. After decoding is complete, print the decoded message. Use the following key for decoding.Convert any numeric characters in input (0-9) to a space in the output. Upper case letters in input/message string are decoded by adding 19 to their ASCII/Integer value. ...
10
2118
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes #these are the operations + = , - = , * = , 1/ = only if 0 not in .
3
5067
by: majorecono | last post by:
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please. Program should.... 1. Ask user to type in a sentence, using JOptionPane.showInputDialog(). 2. The program will scan each letter in the string and count how many times the upper case letter 'E' appears, and how many times the lower case letter 'e' appears. 3. Using a JOptionPane.showInputDialog(),...
0
1246
by: yabighelen | last post by:
HELP!!!!! I have to write a program that does this: Create a program that encrypts and decrypts a message... Inputs: A typed message Output: When the encrypt button is clicked the input message is encrypted. When the Decrypt button is clicked, the message is decrypted.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.