473,811 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the python way?

Hi All,

I've been lurking the list for a month and this is my first post. I am
hoping this post is appropriate here, otherwise, my apologies.

I'm somewhat new to Python, (I'm reading all the tutorials I can find,
and have read through Andre Lessa's Developers Handbook.)
I am trying to learn the Python way of thinking as well as the syntax.

I popped this bit of code together for fun, based on a previous post
regarding randomizing a word.
This shuffles a word, then splits out the vowels and then reassembles
it with the vowels interpolated between consonants.
(To create plausible sounding gibberish)

The code just seems kind of bulky to me. I am wondering, is this an
efficient way to do things, or am I making things harder than
necessary?

#--------------------------begin code------------------
"""scramble s a word, but creates plausable gibberish"""
import random
def shuffled(s):
""" scrambles word"""
l = list(s)
random.shuffle( l)
return ''.join(l)

def contains(alist, b):
"""...is letter b in list a..."""
ret = []
for all in alist:
#print all
if all==b:
return 1
return 0

def newZip(a1,a2):
""" reassemble """
l1=len(a1)
l2=len(a2)

longest = [a1,a2][l1<l2]
shortest = [a1,a2][longest == a1]
diff = max(l1,l2)-min(l1,l2)
#print longest
seq = len(longest)
ret = ""
for j in range(seq):
if len(longest)>0:
ret = ret + longest.pop()
if len(shortest)>0 :
ret = ret + shortest.pop()
return ret

def reinterpolate(w ord):
""" main function """
wlist = shuffled(list(w ord))
vlist = list('aeiouy') # ok, y isn't really a vowel, but...
vees = filter(lambda x: contains(vlist, x),wlist)
cons = filter(lambda x: not(contains(vl ist,x)),wlist)
a=list(vees)
b=list(cons)
return newZip(a,b)

word = "encycloped ia"
print reinterpolate(w ord)

#-------------------------------end code---------------------------

BTW: I'm just curious, is there an easier way to copy-paste code
snippets from the interpreter so that it removes the '... ' and the
'>>> ' from the examples? I'm using <cntrl-H> search and replace now
which works, but if there is a better way, I'd like to know.

thanks in advance,
-J

Jul 19 '05
10 1506
On Mon, 06 Jun 2005 14:52:04 -0400, rumours say that Kent Johnson
<ke****@tds.net > might have written:
def newZip(a1,a2):
""" reassemble """
l1=len(a1)
l2=len(a2)
longest, shortest = [[a1,a2], [a2,a1]][l1<l2]

<snip>

Other ways to write the last line:
----------------------------------

/a/ execution in 22.5% of the original time taken

longest, shortest = l1 < l2 and (a2,a1) or (a1,a2)
/b/ execution in 12.5% of the original time taken

if l1 < l2: longest, shortest= a2, a1
else: longest, shortest= a1, a2
/c/ execution in 6.5% of the original time taken

if l1 < l2: longest= a2; shortest= a1
else: longest= a1; shortest= a2
Another exotic way to write the last *three* lines, running in 228.7% of
the original time:-)

shortest, longest=sorted([a1,a2], key=len)
Numbers from 2.4c1 on a Pentium-M running at 600 MHz (battery powered).
Suggestion /a/ has the advantage of no repetion of the names like the
original line.
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
9734
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
9607
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,...
0
10653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10395
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9211
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
7674
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
5564
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
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3027
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.