Python Forum
a better way of writing code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a better way of writing code
#6
Here's my preferred solution:
vowels = set("aeiou")
sum(char in vowels for char in s)
This takes advantage of Python letting you sum() booleans. I think it reads like English and is Pythonic. (In a larger program, I'd probably name VOWELS as a constant.)

I avoid some particular things for some particular reasons...

I avoided count() because it has to be called for each vowel, causing a multiplication when calculating such algoritms' runtimes. I used a set because they're constant-time lookup, and usually fast; if I really cared about the speed, I'd test to see if a linear search through the string is faster. And if my manager has told me to speed it up I'd check tuples too :)

I avoid map() in Python because I think comprehensions are clearer, and because I think map() should be a method rather than a global function. (This is one place Scala beats Python in my mind. In Python, nesting comprehensions or map calls sucks. But collection.map(foo).map(bar).map(baz) can be spread across multiple lines nicely, if the functions are are defined in-line.)

I avoided len() because it requires creating an intermediate collection. sum() can be passed a generator expression which uses constant-time memory.
Reply


Messages In This Thread
a better way of writing code - by Than999 - Jun-12-2020, 06:22 PM
RE: a better way of writing code - by Yoriz - Jun-12-2020, 06:28 PM
RE: a better way of writing code - by buran - Jun-12-2020, 06:33 PM
RE: a better way of writing code - by bowlofred - Jun-12-2020, 06:45 PM
RE: a better way of writing code - by snippsat - Jun-12-2020, 08:08 PM
RE: a better way of writing code - by micseydel - Jun-12-2020, 08:22 PM
RE: a better way of writing code - by pyzyx3qwerty - Jun-13-2020, 08:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  writing and running code in vscode without saving it akbarza 1 492 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  Help with writing or plan part of code Merlin_1 1 1,899 Aug-24-2020, 03:28 AM
Last Post: Larz60+
  Writing a basic shift code djwilson0495 2 2,351 Aug-16-2020, 01:52 PM
Last Post: djwilson0495
  code not writing to projNameVal portion of code. umkc1 1 1,732 Feb-05-2020, 10:05 PM
Last Post: Larz60+
  C API: Writing Executed Code to a File myanrueller 0 1,758 Nov-17-2019, 09:35 PM
Last Post: myanrueller
  i am getting error while writing code for exception logging rkgupta51179 1 1,921 Nov-03-2019, 05:12 AM
Last Post: buran
  Writing a code with 3 digit numbers while they cant contain 0 and then sum them AFKManager 2 2,499 Jan-13-2019, 12:00 PM
Last Post: perfringo
  Help with Code (Writing Data File) caroline_d_124 3 3,001 Jan-06-2019, 09:17 PM
Last Post: caroline_d_124
  Help writing code to create a ranked list swilson421 2 2,541 Jul-16-2018, 04:51 PM
Last Post: swilson421
  Need help writing simple code isashay 7 6,340 Mar-13-2018, 12:26 AM
Last Post: isashay

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020