fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • Add an element to a set

    When dealing with lists, it was possible to add an element with .append() method, which would add a new element to the end of the list. When working with sets, it’s important to remember that sets don’t have a particular order, so the word append would not exactly fit its purpose. So, adding an element to a set is done thorough the .add() method.
    a = {1, 2}
    a.add(5)
    a.add(8)
    print(a)   # {8, 1, 2, 5}
    If we try to add an element with append, Python will tell us that it’s not possible: AttributeError: 'set' object has no attribute 'append'.

    Challenge

    Given a text, you are asked to write a program that would print the number of unique characters in that text before every position (character ranges span: 1, 1..2, 1..3, 1..4, 1..5, … 1..n).
    The input contains a single line.
    The program should print the number of unique characters for every position of that line separated by a space.
    Input
    Output
    abcabd
    1 2 3 3 3 4
    234asd123
    1 2 3 4 5 6 7 7 7
    To check your solution you need to sign in
    Sign in to continue