# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + id="KXkro8-7bTDc"
def chickens(count):
    return f"Number of chickens: {count}" if count < 10 else "Number of chickens: many"


# + id="-Bzoj_dxbab4"
def string_both_ends_3(s):
    return s if len(s) < 3 else s[:3]+s[len(s)-3:]


# + id="89-n4vxQbcQ2"
def first_char_replace(s):
    return s[0]+s[1:].replace(s[0].lower(), '@').replace(s[0].upper(), '@')


# + id="2kbjAlDBbeLl"
def string_jumble(a, b):
    return b[:2]+a[2:]+' '+a[:2]+b[2:]


# + id="7yLC3ffrbges"
def match_first_last(words):
    return sum([1 for s in words if len(s) >= 2 and s[0] == s[-1]])


# + id="-KSpx8tDbiNr"
def group_strings(words):
  return sorted(words)


# + id="bbdfkeD7bmFr"
def sort_last(tuples):
    return sorted(tuples, key=lambda x: x[1])


# + id="T8E4jaD5bnzK"
def main():
  print ('Number of chickens')
  print(chickens(4))
  print(chickens(9))
  print(chickens(10))
  print(chickens(99))

  print ('\n3 characters from both ends')
  print(string_both_ends_3('spring'))
  print(string_both_ends_3('Intelligence'))
  print(string_both_ends_3('a'))
  print(string_both_ends_3('xyz'))

  print ('\nReplace occurrences of first character')
  print(first_char_replace('babble'))
  print(first_char_replace('aardvark'))
  print(first_char_replace('google'))
  print(first_char_replace('Ooogle'))

  print ('\nString Jumble')
  print(string_jumble('mix', 'pod'))
  print(string_jumble('dog', 'dinner'))
  print(string_jumble('gnash', 'sport'))
  print(string_jumble('pezzy', 'firm'))

  print ('\nMatching first and last characters')
  print(match_first_last(['aba', 'xyz', 'aa', 'a', 'bbb']))
  print(match_first_last(['', 'x', 'ay', 'ayx', 'ax']))
  print(match_first_last(['aaa', 'be', 'abc', 'aello']))

  print ('\nGroup string in a list')
  print(group_strings(['bbb', 'ccc', 'axx', 'xzz', 'aaa']))
  print(group_strings(['ccc', 'abb', 'aaa', 'xcc', 'aaa']))
  print(group_strings(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']))

  print ('\nsort_last')
  print(sort_last([(1, 3), (3, 2), (2, 1)]))
  print(sort_last([(2, 3), (1, 2), (3, 1)]))
  print(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]))


# + id="cAmJUqYJbpwR"
'''
Python files .py are modules. Modules can define variables, functions, and classes.
When a Python interpreter reads a Python file, it first sets a few special variables.
Then it executes the code from the file. 

One of those variables is called __name__.

When the interpreter runs a module, the __name__ variable will be set as  __main__ 
if the module that is being run is the main program.

If the code is importing the module from another module, then the __name__  
variable will be set to that module’s name.
'''
# Standard boilerplate to call the main() function.
if __name__ == '__main__':
  main()

# +
# 6.8
import numpy as np
import matplotlib.pyplot as plt

def IIIDarrToImage(IIID):
    IIID=IIID.astype(np.uint8)
    plt.imshow(IIID)
    plt.imsave("my.png",IIID)

IIIDarrToImage(np.array([
    [[255, 50, 100], [255, 250, 100], [250, 50, 250]],
    [[255, 250, 100], [255, 0, 0], [0, 0, 250]],
    [[255, 50, 100], [255, 250, 100], [250, 250, 250]]]))


# +
# 6.9
import pandas as pd

# use this link if you have file
df = pd.read_csv('train.csv')
#else
# df = pd.read_csv('https://raw.githubusercontent.com/ghostdart/Artificial-Intelligence-Labs/main/02-Intro-to-numpy-and-pandas/train.csv')

print("Age between 18 and 30")
print(len(df[(df['Age'] < 30)
             & (df['Age'] > 18)]))

# -

print("Females survivers age between 18 and 30")
print(len(df[(df['Sex'] == 'female')
             & (df['Age'] < 30)
             & (df['Age'] > 18)
             & (df['Survived'] == 1)]))

StarcoderdataJupyterScriptsDedupFiltered