Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7702633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:16:16+00:00 2026-05-31T23:16:16+00:00

I have joined Standford online course on Algorithms design and now I’m solving the

  • 0

I have joined Standford online course on Algorithms design and now I’m solving the first programming question.

The file contains all the 100,000 integers between 1 and 100,000
(including both) in some random order(no integer is repeated). Your
task is to find the number of inversions in the file given (every row
has a single integer between 1 and 100,000). Assume your array is from
1 to 100,000 and i-th row of the file gives you the i-th entry of the
array.

Update: I have found that my code works only for the 2^n case. Problem is in the code, not Python. I have updated the code, now it work fine and I have done the quiz. Thanks to all who helped

Fixed code is:

 def merge_count_split (a, b):
        c = []
        inv = 0
        i=0
        j=0
        for k in range( len(a) + len(b) ):
                if i < len(a) and j < len(b):
                        if a[i] < b[j]:
                                c.append(a[i])
                                i += 1
                        elif a[i] > b[j]:
                                c.append(b[j])
                                inv += len(a)-i
                                j += 1
                elif i == len(a):
                        c.append(b[j])
                        j += 1
                elif j == len(b):
                        c.append(a[i])
                        i += 1
        return c, inv

def count_inv (data):
        n = len(data)
        if n == 1:
                return data, 0
        a, x = count_inv(data[:n/2])
        b, y = count_inv(data[n/2:])
        c, z = merge_count_split(a,b)
        return c, x + y + z

with open('IntegerArray.txt') as f:
        array = [int(line) for line in f]
        print count_inv(array)[0]

This program works fine for small arrays, but for the large array from the question it prints array of 65536 numbers in proper order, not 100000, as I expect. It omits numbers at random places.

What is the reason for this unexpected behaviour of python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:16:18+00:00Added an answer on May 31, 2026 at 11:16 pm

    By setting n = len(a) and merging only n * 2 times, you truncate b if it’s longer than a.

    This partially explains the striking fact that you have 2 ** 16 items in the resulting list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the mapping solution from this question to have a joined component. But
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
I have joined a project recently and now I'm working on its Internationalization improvement.
Currently i have joined together several tables. now i want to select some columns
I am building a photo uploading website and I have joined two tables together
I have two tables that should be joined together by a foreign key relationship,
I have two columns that are joined together on certain criteria, but I would
I have a couple processes running a tool I've written that are joined by
I have two tables (Table A, Table B) joined with a join table (TableAB)
say if I have 10 tables. These tables are joined together on different conditions.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.