DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. My Journey into Python from Scratch - Part Two

My Journey into Python from Scratch - Part Two

Hod Benbinyamin user avatar by
Hod Benbinyamin
·
Mar. 21, 12 · Interview
Like (0)
Save
Tweet
Share
3.93K Views

Join the DZone community and get the full member experience.

Join For Free
Well, I had a crazy week.

My first post My Journey into Python from Scratch was a huge hit, I got all this attention and even now, several days later, I still get visits and comments and am being followed via Email and RSS.  I have read each comment and each referral to a site which led me to many python sites that came to help and found myself in a veritable heaven for a newbie like me.

I also met this guy from Costa-Rica who is going through the same journey as me and we are exchanging ideas by email.  My post was published on some great sites and if you search Google for “Python from Scratch” I am in the Top 5 global results.  All of this caused me to lose focus, and on top of it, my young son is still sick, so I have had some sleepless night lately.

Last night I decided that no matter what I would continue on my journey and I have started to read the next page in the Google python class- lists.  I read the entire chapter, which explains how lists work in Python, a bit tricky at some points yet easy to follow.  Then the chapter moved on to how to use the FOR var IN list (which is different from C++).  It took me some time to get used to it, but once you get it- wow!  Towards the end, you become familiar with some lists method which will be handy later on and voila: the exercise.

So here I am solving the tests and on the first 2 problems I was doing fine, only to bump into the third problem which took me about an hour.  Here it is:

"Given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple." example- (1, 3), (3, 2), (2, 1) should return- (2, 1), (3, 2), (1, 3).

It's easy to see the obstacle here is how to sort using the last element, I really tried to make my code as simple as possible (KISS) but no matter what I did, I was writing more and more rows and taking care of more and more corner cases until I decide that I was doing something wrong and surely there was a faster solution hidden in the learning subject.

So I read the lists page course again until I discovered the problem- Google has a Doc BUG in this page- an important method is mentioned but not explained. It says:

list.sort() -- sorts the list in place (does not return it).
(The sorted() function shown below is preferred.)

But there is no sorted() below and this method was the method to solve the issue easily.  In fact list.sort() can also handle this case but I couldn’t understand it from reading over here.

After spending too much time on this and it being so late at night, I went to sleep.  The following night, I spent another 30 minutes try and solve the advanced exercise and again I ran into a strange situation: the second exercise was very easy to do and took me only 3 rows to complete, yet Google solution was about 10 rows (doesn't make sense, right?)

So I decide to show it to you and ask you where I am wrong in my solution:

The question is:

"Given two lists sorted in increasing order, create and return a merged list of all the elements in sorted order." example- ['aa', 'xx', 'zz'], ['bb', 'cc']) should return- ['aa', 'bb', 'cc', 'xx', 'zz'])

My 3 row solution:
def linear_merge(list1, list2):
	list1.sort (list1.extend (list2))
	return list1

Google's 10 row solution:
def linear_merge(list1, list2):
   result = []
   while len(list1) and len(list2):
    if list1[0] < list2[0]:
      result.append(list1.pop(0))
    else:
      result.append(list2.pop(0))

  result.extend(list1)
  result.extend(list2)
  return result
I was running several lists to test my code and it works just fine.  This also goes fine with The Zen of Python “Simple is better than complex.”  So I am asking you, the Python people, can you find the reason why my code is only three rows while Google’s is much bigger? Do I have a bug? Can you do it better?

Anyway, that's it for this subject and I am all fired-up for the next one as I continue on my journey into Python from Scratch.
Python (language) Database Scratch (programming language)

Published at DZone with permission of Hod Benbinyamin. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Keep Your Application Secrets Secret
  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: