Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • CompanyInterviews >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • CDK Global
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • EPAM
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views
While working with Python or any other programming language always check the version in which you will be coding, as may be some behavior got changed.
Like in Python, there is change in the behavior of Division.
Prior to Python3, 5/2 used to give 2 & 5/2.0 or 5.0/2 or 5.0/2.0 will only give 2.5
But from Python3, 5/2 will give 2.5
So prior to Python3, you must give at least one operand as floating point to get the result as floating point, else Python truncates the result.
===============================================================================================
Check below code to get the idea about Subscript, Superscript, Operation implementations using dunder methods, use of __call__(), use of one line if statement.....

​class Polynomial:
    def __init__(self, *coeffs):
        self.coeffs = coeffs

    # For subscript & superscript notations look for unicode list
    def __repr__(self):
        # return 'Polynomial (*{!r})'.format(self.coeffs) # Python 3+, only a & r work here
        result = ''
        result += f'{self.coeffs[0]}X\u00b2' if self.coeffs[0] != 0 else ""
        val = not str(self.coeffs[1]).startswith('-') if self.coeffs[1] != 0 else ""
        result += ('+'*val+f'{self.coeffs[1]}X')
        val = not str(self.coeffs[1]).startswith('-') if self.coeffs[1] != 0 else ""
        result += ('+'*val+f'{self.coeffs[2]}X')

        if result == '':
            result = '0'
        return result

    def __add__(self, other):
        return Polynomial(*(x+y for x, y in zip(self.coeffs, other.coeffs)))

    def __sub__(self, other):
        return Polynomial(*(x-y for x, y in zip(self.coeffs, other.coeffs)))

    def __mul__(self, other):
        return 'Not Supported'

    def __call__(self, *args):
        if isinstance(args[0], Polynomial):
            return 'Not Supported this way'
        else:
            return 'No such Operation is supported'


# X²+X+X
p1 = Polynomial(5, 3, 2)
p2 = Polynomial(2, 4, 3)

print(p1+p2)
print(p1-p2)
print(p1*p2)
print(p1(p2))
print(p1('7X²+7X+5X'))
print("You might recall a\u00b2 + b\u00b2 = c\u00b2 from your Math lessons, or you like Chemistry for H\u2082O.")

Powered by Create your own unique website with customizable templates.