{"id":21018,"date":"2022-11-16T14:11:47","date_gmt":"2022-11-16T08:41:47","guid":{"rendered":"https:\/\/copyassignment.com\/?p=21018"},"modified":"2022-11-16T14:11:49","modified_gmt":"2022-11-16T08:41:49","slug":"hackerrank-day-4-solution-in-python","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/hackerrank-day-4-solution-in-python\/","title":{"rendered":"HackerRank Day 4 Solution in Python: Class vs Instance"},"content":{"rendered":"\n<p>Today we will see the&nbsp;<strong><em>HackerRank Day 4 Solution in Python<\/em><\/strong>. The problem is named&nbsp;Class vs Instance&nbsp;which is part of&nbsp;<strong><em>30 Days of code on HackerRank<\/em><\/strong>. Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Day 4: Class vs Instance Problem statement<\/h2>\n\n\n\n<p>We have to write a&nbsp;<em>Person<\/em>&nbsp;class with an instance variable,&nbsp;age, and a constructor that takes an integer,&nbsp;initialAge, as a parameter. The constructor must assign&nbsp;initialAge&nbsp;to&nbsp;age&nbsp;after confirming the argument passed as&nbsp;&nbsp;is not negative; if a negative argument is passed as&nbsp;initialAge, the constructor should set&nbsp;age&nbsp;to&nbsp;0&nbsp;and print&nbsp;<code>Age is not valid, setting age to 0.<\/code>. In addition, we should write the following instance methods:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><em>yearPasses()<\/em>&nbsp;should increase the&nbsp;age&nbsp;instance variable by&nbsp;1.<\/li>\n\n\n\n<li><em>amIOld()<\/em>&nbsp;should perform the following conditional actions:\n<ul class=\"wp-block-list\">\n<li>If&nbsp;age&lt;13, print&nbsp;<code>You are young.<\/code>.<\/li>\n\n\n\n<li>If&nbsp;age&gt;=13&nbsp;and&nbsp;age&lt;18, print&nbsp;<code>You are a teenager.<\/code>.<\/li>\n\n\n\n<li>Otherwise, print&nbsp;<code>You are old.<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Sample Input<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4\n-1\n10\n16\n18<\/code><\/pre>\n\n\n\n<p><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Age is not valid, setting age to 0.\nYou are young.\nYou are young.\n\nYou are young.\nYou are a teenager.\n\nYou are a teenager.\nYou are old.\n\nYou are old.\nYou are old.<\/code><\/pre>\n\n\n\n<p>You can solve the problem&nbsp;<a href=\"https:\/\/www.hackerrank.com\/challenges\/30-2d-arrays\/problem\" target=\"_blank\" rel=\"noreferrer noopener\">h<\/a><a href=\"http:\/\/hackerrank.com\/challenges\/30-class-vs-instance\/problem\" target=\"_blank\" data-type=\"URL\" data-id=\"hackerrank.com\/challenges\/30-class-vs-instance\/problem\" rel=\"noreferrer noopener\">ere<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HackerRank Day 4 Solution in Python<\/h2>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">class Person:\n    def __init__(self,initialAge):\n        #Check is age is negative\n        if initialAge&lt;0:\n            print(\"Age is not valid, setting age to 0.\")\n            self.age=0\n        else:\n            self.age=initialAge\n    \n    #Method to perform given conditional statements \n    def amIOld(self):\n        #If age less than 13\n        if self.age&lt;13:\n            print(\"You are young.\")\n        #If age greater than 13 and lesser than 18\n        elif self.age>=13 and self.age&lt;18:\n            print(\"You are a teenager.\")\n        #If age greater than 18\n        elifc self.age>18:\n            print(\"You are old.\")\n    \n    #Method to increment age\n    def yearPasses(self):\n        self.age+=1\n        \nt = int(input())\nfor i in range(0, t):\n    age = int(input())         \n    p = Person(age)  \n    p.amIOld()\n    for j in range(0, 3):\n        p.yearPasses()       \n    p.amIOld()\n    print(\"\")<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Code Explanation<\/h2>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<ul class=\"wp-block-list\">\n<li>After getting the input, create an object for the Person class with input age as a parameter<\/li>\n\n\n\n<li>Then in the constructor, check if the age is negative, then print Age is not valid, setting age to 0 and then setting the age value to 0. Else initialize age with the corresponding value<\/li>\n\n\n\n<li>in the amIOld method check if the age is less than 13 and print You are young.<\/li>\n\n\n\n<li>Then check if the age is greater than 13 and less than 18, then print You are a teenager.<\/li>\n\n\n\n<li>If the age is greater than 18 then print You are old<\/li>\n<\/ul>\n\n\n\n<div style=\"text-align:center\" class=\"wp-block-atomic-blocks-ab-button ab-block-button\"><a href=\"https:\/\/copyassignment.com\/top-100-python-projects-with-source-code\/\" class=\"ab-button ab-button-shape-rounded ab-button-size-medium\" style=\"color:#ffffff;background-color:#3373dc\">Best 100+ Python Projects with source code<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-8-solution-in-python-dictionaries-and-maps\/\">HackerRank Day 8 Solution in Python: Dictionaries and Maps<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-7-solution-in-python-arrays\/\">HackerRank Day 7 Solution in Python: Arrays<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-6-solution-in-python-lets-review\/\">HackerRank Day 6 Solution in Python: Let&#8217;s review<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-5-solution-in-python-loops\/\">HackerRank Day 5 Solution in Python: Loops<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-4-solution-in-python\/\">HackerRank Day 4 Solution in Python: Class vs Instance<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-3-solution-in-python\/\">HackerRank Day 3 Solution in Python: Intro to Conditional Statements<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-2-solution-in-python-operators\/\">HackerRank Day 2 Solution in Python: Operators<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-1-solution-in-python-data-types\/\">HackerRank Day 1 Solution in Python: Data Types<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-0-solution-in-python-hello-world\/\">HackerRank Day 0 Solution in Python: Hello World<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-29-solution-in-python-bitwise-and\/\">HackerRank Day 29 Solution in Python: Bitwise AND<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-28-solution-in-python-regex-patterns-and-intro-to-databases\/\">HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-27-solution-in-python-testing\/\">HackerRank Day 27 Solution in Python: Testing<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-26-solution-in-python-nested-logic\/\">HackerRank Day 26 Solution in Python: Nested Logic<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-25-solution-in-python-running-time-and-complexity\/\">HackerRank Day 25 Solution in Python: Running Time and Complexity<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-24-solution-in-python-more-linked-lists\/\">HackerRank Day 24 Solution in Python: More Linked Lists<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-23-solution-in-python-bst-level-order-traversal\/\">HackerRank Day 23 Solution in Python: BST Level Order Traversal<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-22-solution-in-python-binary-search-trees\/\">HackerRank Day 22 Solution in Python: Binary Search Trees<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-20-solution-in-python-sorting\/\">HackerRank Day 20 Solution in Python: Sorting<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-19-solution-in-python-interfaces\/\">HackerRank Day 19 Solution in Python: Interfaces<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-18-solution-in-python-queues-and-stacks\/\">HackerRank Day 18 Solution in Python: Queues and Stacks<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-17-solution-in-python-more-exceptions\/\">HackerRank Day 17 Solution in Python: More Exceptions<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-16-solution-exceptions-string-to-integer\/\">HackerRank Day 16 Solution: Exceptions &#8211; String to Integer<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-15-solution-in-python-linked-list\/\">HackerRank Day 15 Solution in Python: Linked List<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-13-solution-in-python-abstract-classes\/\">HackerRank Day 13 Solution in Python: Abstract Classes<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-14-solution-in-python-scope\/\">HackerRank Day 14 Solution in Python: Scope<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-12-solution-in-python-inheritance\/\">HackerRank Day 12 Solution in Python: Inheritance<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-11-solution-in-python-2d-arrays\/\">HackerRank Day 11 Solution in Python: 2D Arrays<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hackerrank-day-10-solution-in-python-binary-numbers\/\">HackerRank Day 10 Solution in Python: Binary Numbers<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Today we will see the&nbsp;HackerRank Day 4 Solution in Python. The problem is named&nbsp;Class vs Instance&nbsp;which is part of&nbsp;30 Days of code on HackerRank. Let\u2019s&#8230;<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1932,22,1306],"tags":[],"class_list":["post-21018","post","type-post","status-publish","format-standard","hentry","category-30-days-of-code","category-allcategorites","category-competitive-programming","wpcat-1932-id","wpcat-22-id","wpcat-1306-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/21018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=21018"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/21018\/revisions"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=21018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=21018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=21018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}