0

Using

if(array[i]=="1")

passes all test cases (in Hacker rank).

But

if(array[i]=='1')

doesn't pass all the test cases.

What are the differences between "" and '' in Python?
Why does the latter take more time?

Here are the screenshots:

Using single-quotes:
HackerRank code and error using double-quotes

Using double-quotes:
HackerRank code and error using single-quotes

The code:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'acmTeam' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts STRING_ARRAY topic as parameter.
#

def acmTeam(topic):
    m=0
    count=0
    for i in range(len(topic)):
        for j in range(i+1,len(topic)):
            k=(bin(int(topic[i],2) | int(topic[j],2)))[2:]
            # if k>m:
            #     m=k
            #     count=1
            # elif m==k:
            #     count+=1
            val=0
            for ind in range(len(k)):
                if (k[ind]=="1"):
                    val+=1
            if val>m:
                count=1
                m=val
            elif val==m:
                count+=1

    return [m,count]
               
Amish Gupta
  • 139
  • 8
  • 8
    "" and '' do exactly the same thing. Presumably your code's average time is on the border between fail and pass, so whether it passes or fails depends on external conditions. – SuperStormer Jul 02 '22 at 02:49
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) Show your code as text, copied and pasted from your IDE, with [proper formatting](https://stackoverflow.com/help/formatting). We [will not transcribe it for you](https://meta.stackoverflow.com/questions/415040). – Karl Knechtel Jul 02 '22 at 03:10
  • There are many ways the code could be improved; however, without a *specific, objective* question (perhaps something like "How can I improve the runtime of this code?" we cannot do anything for you. – Karl Knechtel Jul 02 '22 at 03:14
  • The original question isn't about that specific code or to improve that code, it was to ask why the there's a time difference, The screenshots were just served as proofs. However I understand if you wish to run that specific code, I'll edit to make the changes. Thanks. – Amish Gupta Jul 02 '22 at 10:55

2 Answers2

2

There's no difference.

If we view the bytecode for comparing single quoted vs double quoted comparisons using the dis package:

Single Quoted

>>> dis.dis(lambda: input() == 'A')
  1           0 LOAD_GLOBAL              0 (input)
              2 CALL_FUNCTION            0
              4 LOAD_CONST               1 ('A')
              6 COMPARE_OP               2 (==)
              8 RETURN_VALUE

Double Quoted

>>> dis.dis(lambda: input() == "A")
  1           0 LOAD_GLOBAL              0 (input)
              2 CALL_FUNCTION            0
              4 LOAD_CONST               1 ('A')
              6 COMPARE_OP               2 (==)
              8 RETURN_VALUE

Python's lexical analyzation process

Using Python's ast module, we can look at our code before it's compiled.

Single Quoted

>>> print(ast.dump(ast.parse('a == \'hello\'', mode='eval'), indent=4))
Expression(
    body=Compare(
        left=Name(id='a', ctx=Load()),
        ops=[
            Eq()],
        comparators=[
            Constant(value='hello')]))

Double Quoted

>>> print(ast.dump(ast.parse('a == "hello"', mode='eval'), indent=4))
Expression(
    body=Compare(
        left=Name(id='a', ctx=Load()),
        ops=[
            Eq()],
        comparators=[
            Constant(value='hello')]))

Conclusion

Probably an incorrect bit in Python's evaluator.

The code is the exact same either way, and there's no real way of knowing if this is coincidence without inspecting every last detail of the interpreter's C implementation.

ImajinDevon
  • 287
  • 1
  • 11
1

I don't know why one happens and the other doesn't. The times vary in each execution.

Regarding the double and single quotes, in python there are no differences, you can find this in countless web pages. Unfortunately I couldn't find it on the official page http://www.python.org/