Professional Documents
Culture Documents
Python - Module Test - Jupyter Notebook
Python - Module Test - Jupyter Notebook
In [1]:
# Write a program which takes 2 digits, X, Y as input and generates a 2-dimensional array.
# The element value in the ith row and jth column of the array should be i*j.
# Example:
# Suppose the following inputs are given to the program: 3,5
x = int(x)
y = int(y)
return matx
[0, 0, 0, 0, 0]
[0, 1, 2, 3, 4]
[0, 2, 4, 6, 8]
In [2]:
# Given a number, write a function to output its reverse digits. (e.g., given 123 the answe
# Make sure that if it is a negative number, you keep the negative in the front (-123 becom
def reverse_number(num):
num = str(num)
if num[0] == "-":
revnum = int('-' + num[-1:0:-1])
return revnum
else:
revnum = int(num[::-1])
return revnum
print(reverse_number(12345))
print(reverse_number(-12345))
54321
-54321
In [3]:
# You are given a sentence and want to shift each letter by 2 in alphabet to create a secre
# The sentence you want to encode is "the lazy dog jumped over the quick brown fox"
# and the output should be ’vjg ncba fqi lworgf qxgt vjg swkem dtqyp hqz’
numl = {}
lnum = {}
inum = 0
for i in letters:
numl[inum] = i
lnum[i] = inum
inum = inum + 1
if newp == 26:
newp = 0
elif newp == 27:
newp = 1
return encodedtext
text = "the lazy dog jumped over the quick brown fox"
switchl(text, 2)
Out[3]:
'vjggncbaafqiilworgffqxgttvjggswkemmdtqypphqz'
In [4]:
def leapyearcheck(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
print(leapyearcheck(2000))
print(leapyearcheck(1900))
print(leapyearcheck(2022))
True
False
False
In [5]:
# Consider a number N. Take all the numbers from 1 to N*2, and split them in two lists, wit
# Example:
# If N is 3, then the output lists are [1,3,5] and [2,4,6]
oddlist = []
evenlist = []
print(separatenums(1, 3))
In [6]:
# Extract the data from the below API for below given Lat and Lon in json format, normalize
# Endpoint/URL : http://www.7timer.info/bin/astro.php
# Method : GET
# Variables : lon, lat, ac=0, lang=en, unit=metric, tzshift=0, output=json
# lat lon
# 57.9 2.3
# 34.55 138.4
# 36.5 3.5
import requests
import json
import urllib
import pandas as pd
url = 'http://www.7timer.info/bin/astro.php'
def apitojson(url):
response = urllib.request.urlopen(url)
data = json.loads(response.read())
return data
# print(apitojson(url))
In [7]:
# Read the file as a Pandas DataFrame and create another DataFrame with the following colum
# ID – No change
# NAME – New column by concatenating FIRST_NAME and LAST_NAME
# PHONE_NUMBER – Replace all ‘.’ with ‘-’
import pandas as pd
import numpy as np
df = pd.read_csv(file)
df = pd.DataFrame(df)
finaldf = df
print(finaldf.head())
C:\Users\PGosavi\AppData\Local\Temp\ipykernel_10212\3838715516.py:30: Future
Warning: The default value of regex will change from True to False in a futu
re version. In addition, single character regular expressions will *not* be
treated as literal strings when regex=True.