question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Numbers Concept Exercise]: Rethink/Redesign

See original GitHub issue

Per issues #2946 , #2621 , #2835, #2804, #2591, and #2490 – this exercise has gone through a lot of improvements and also complaints.

Since multiple re-works and improvements have not succeeded, we have decided a complete re-design of the Numbers exercise is in order.

Concept docs can be retained, or revised as needed. Design documents should be updated.

Original specs for Numbers exercise are below:


Goal

The goal of this exercise is to teach the basics of theint , float , and complex numeric types (numbers) in Python.

Learning objectives

  • understand the difference between int, float and complex numbers in Python
  • understand that Python uses j to represent the square root of -1. appending j to a number defines it as imaginary. (e.g.3j is equivalent of 3(sqrt(-1))IF Pythons math module allowed negative square roots…it doesn’t).
  • understand that a decimal (.) preceeded or followed by a number creates a float literal
  • create an imaginary literal via appending j to an int or float (e.g. 3.4j or 5j)
  • create a float by casting an int and an int by casting a float, using the int() and float() constructors.
  • create complex numbers by casting int and float to complex using the complex() constructor.
  • create a complex number by adding a float and an imaginary literal (e.g. 3.0 + 6j)
  • understand when and how Python uses arithmetic conversions to apply arithmetic operators between these numeric types.
  • apply the arithmetic operators to each/between each of the number types (where applicable)
  • perform integer (floor) division and “regular” (float) division and see how they differ.
  • changing precision of a float, using round(<float>, <digits>)

Out of scope

  • Special values for Infinity and NaN
  • Special math functions for complex numbers contained in cmath
  • Bitwise operations on ints
  • round numbers using math.floor() and math.ceiling()
  • Issues with precision limits & sys.float_info – but maybe we should note these in passing, or link to them in links.
  • Numpy and the numeric types defined by Numpy – again, something for links/notes.
  • Checking strings to see if they are floats. This means any test input to this exercise should not require a student to (a) catch or raise an Exception if a non-float string is passed, or (b) check to see if the string is a float. Only pass valid float strings, i.e. "1.032".

Concepts

  • numbers
  • arithmetic operations

Prerequisites

  • basics

Resources to refer to

Hints

Hints can link to the builtin function docs mentioned above, with appropriate prompts.

Concept Description

(a variant of this can be used for the v3/languages/python/concepts/<concept>/about.md doc and this exercises introduction.md doc.)

Python has three different types of built-in numbers: integers (int), floating-point (float, and complex (complex). Fractions (fractions.Fraction) and Decimals (decimal.Decimal) are also available via import from the standard library.

Whole numbers (including hex, octals and binary numbers) without decimal places are identified as ints:

#whole number
>>> 1234
1234
>>> type(1234)
<class 'int'>

>>>  -12
-12

#hex number
>>> 0x17
23
>>> type(0x17)
<class 'int'>

#octal number
>>> 0o446
294
>>> type(0o446)
<class 'int'>

#binary number
>>> 0b1100110
102
>>> type(0b1100110)
<class 'int'>

Numbers containing a decimal point are identified as floats:

>>> 3.45
3.45
>>> type(3.45)
<class 'float'>

Appending `j` or `J` to a number creates a _imaginary number_ -- a `complex` number with a zero real part. Integers or floats can then be added to an imaginary number to create a `complex` number with both real and imaginary parts:

>>> 3j
3j
>>> type(3j)
<class 'complex'>

>>> 3.5+4j
(3.5+4j)

Arithmetic

Python fully supports arithmetic between these different number types, and will convert narrower numbers to match their less narrow counterparts when used with binary arithmetic operators (+, -, *, /, and %). ints are narrower than floats, which are considered narrower than complex. Comparisons between different number types behaves as as if the exact values of those numbers were being compared:

#the int is widened to a float here, and a float is returned
>>> 3 + 4.0
7.0

#the int is widened to a complex number, and a complex number is returned
>>> 6/(3+2j)
(2+2j)

#division always returns a float, even if integers are used
>>> 6/2
3.0

#if an int result is needed, you can use floor division to truncate the result
>>> 6//2
3

#when comparing, exact values are used
>>> 23 == 0x17
True

>>> 0b10111 \== 0x17
True

>>> 6 == (6+0j)
True

All numbers (except complex) support the same general arithmetic operations, evaluated according to operator precedence.

Precision & Representation

Integers in Python have arbitrary precision – the amount of digits is limited only by the available memory of the host system.

Floating point numbers are usually implemented using a double in C (15 decimal places of precision), but will vary in representation based on the host system. Complex numbers have a real and an imaginary part, both of which are represented by floating point numbers.

For a more detailed discussion of the issues and limitations of floating point arithmetic, take a look at The Python Tutorial.

Implementing

Tests should be written using unittest.TestCase, and the test file named numbers_test.py.

Code in the .meta/example.py file should only use syntax & concepts introduced in this exercise or one of its prerequisites.
Please do not use comprehensions, generator expressions, or other syntax not previously covered. Please also follow PEP8 guidelines.

Help

If you have any questions while implementing the exercise, please post the questions as comments in this issue.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:16 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
DjangoFettcommented, Feb 21, 2022

I was preparing to work on this. Commenting here for the ticket to be assigned

1reaction
mohmad-nullcommented, Mar 1, 2022

But I don’t think having a debate about the utility of complex numbers (or any other numeric type) in programming is helpful. Nor do I want to have a debate about which math is “too complicated” or “unnecessary”. I am interested in crafting exercises that don’t excessively confuse or frustrate students and (hopefully) help then become more fluent in Python.

By definition if the math is too complex then you will be creating exercises that are going to confuse and frustrate students who do not have that math. See also my comments in this thread: https://github.com/exercism/problem-specifications/issues/1902 which are highly pertinent.

I am also going to ask you going forward to either PR a concept exercise for numbers you feel is better or stop complaining about the existing one.

As noted in my other comment, I’d genuinely forgotten I’d raised the issues months previously; I wasn’t being vexatious. The fact they came up again when another mentee did them 3 months later only re-enforces my point,

As clearly stated at the top of the description (emphasis mine): Design documents should be updated.

Sorry, but I don’t know what that means. If you’re saying “we don’t want feedback at this point”, then sure, I’ll wait.

Read more comments on GitHub >

github_iconTop Results From Across the Web

High School Redesign: How to Apply the XQ School Design ...
Want to change high school? Use XQ's suite of materials to explore what high schools can be—and should be—for American students.
Read more >
Rethink space: (Re)designing a workspace using human ...
The redesigned workspace embraces the differences among MICHR's teams ... spread the ideas but also acted as a consensus-building exercise.
Read more >
Answers to the Most Frequently Asked Questions about ...
First, it is about reengineering processes. In a fundamental way, most processes are dysfunctional and need to be redesigned, starting with the core,...
Read more >
6 Ways the Workplace Will Change in the Next 10 Years
Enabling a workplace that is innovative, inclusive and insightful demands a strategy to rethink how work gets done. Consider these six ideas ......
Read more >
One School's Approach To Rethink And Redesign Discipline ...
rethink and redesign school discipline practices is long overdue. ... Developing the Definition of Restorative Justice.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found