Software Development Exam  >  Software Development Notes  >  Python- Mastering Development in Python  >  Mastering Development in Python: Assignment 1

Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development PDF Download

Q1: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9

Output: [0,1]

Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6

Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6

Output: [0,1]

 Constraints:

2 <= nums.length <= 104

-109 <= nums[i] <= 109

-109 <= target <= 109

Ans:

Intuition

The Two Sum problem asks us to find two numbers in an array that sum up to a given target value. We need to return the indices of these two numbers.

Approach

  • One brute force approach is to consider every pair of elements and check if their sum equals the target. This can be done using nested loops, where the outer loop iterates from the first element to the second-to-last element, and the inner loop iterates from the next element to the last element. However, this approach has a time complexity of O(n^2).
  • A more efficient approach is to use a hash table (unordered_map in C++). We can iterate through the array once, and for each element, check if the target minus the current element exists in the hash table. If it does, we have found a valid pair of numbers. If not, we add the current element to the hash table.

Approach using a hash table:

  • Create an empty hash table to store elements and their indices.
  • Iterate through the array from left to right.
  • For each element nums[i], calculate the complement by subtracting it from the target: complement = target - nums[i].
  • Check if the complement exists in the hash table. If it does, we have found a solution.
  • If the complement does not exist in the hash table, add the current element nums[i] to the hash table with its index as the value.
  • Repeat steps 3-5 until we find a solution or reach the end of the array.
  • If no solution is found, return an empty array or an appropriate indicator.
  • This approach has a time complexity of O(n) since hash table lookups take constant time on average. It allows us to solve the Two Sum problem efficiently by making just one pass through the array.

 Code: 

Brute Force Approach: 

Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development


Solution 2: (Two-pass Hash Table)

Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development


Q2: 

Given an integer x, return true if x is a  palindrome , and false otherwise.

Example 1:

Input: x = 121

Output: true

Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121

Output: false

Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10

Output: false

Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

 Constraints:

-231 <= x <= 231 - 1

Code: 

Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development



Q3: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value

I             1

V             5

X             10

L             50

C             100

D             500

M             1000

For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9. 

X can be placed before L (50) and C (100) to make 40 and 90. 

C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

Example 1:

Input: s = "III"

Output: 3

Explanation: III = 3.

Example 2:

Input: s = "LVIII"

Output: 58

Explanation: L = 50, V= 5, III = 3.

Example 3:

Input: s = "MCMXCIV"

Output: 1994

Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 Constraints:

1 <= s.length <= 15

s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').

It is guaranteed that s is a valid roman numeral in the range [1, 3999].

 Code: Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development



Q4:  Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: strs = ["flower","flow","flight"]

Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]

Output: ""

Explanation: There is no common prefix among the input strings.

 Constraints:

1 <= strs.length <= 200

0 <= strs[i].length <= 200

strs[i] consists of only lowercase English letters.

Ans: 

  • This code implements the longestCommonPrefix function that takes a list of strings v as input and returns the longest common prefix of all the strings. Here is an explanation of how the code works:
  • Initialize an empty string ans to store the common prefix.
  • Sort the input list v lexicographically. This step is necessary because the common prefix should be common to all the strings, so we need to find the common prefix of the first and last string in the sorted list.
  • Iterate through the characters of the first and last string in the sorted list, stopping at the length of the shorter string.
  • If the current character of the first string is not equal to the current character of the last string, return the common prefix found so far.
  • Otherwise, append the current character to the ans string.
  • Return the ans string containing the longest common prefix.
  • Note that the code assumes that the input list v is non-empty, and that all the strings in v have at least one character. If either of these assumptions is not true, the code may fail.

Code:Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development 


Q5:  Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.

Open brackets must be closed in the correct order.

Every close bracket has a corresponding open bracket of the same type.

 Example 1:

Input: s = "()"

Output: true

Example 2:

Input: s = "()[]{}"

Output: true

Example 3:

Input: s = "(]"

Output: false

 Constraints:

1 <= s.length <= 104

s consists of parentheses only '()[]{}'.

Ans: Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development

The document Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development is a part of the Software Development Course Python- Mastering Development in Python.
All you need of Software Development at this link: Software Development
8 videos|5 docs

Top Courses for Software Development

FAQs on Mastering Development in Python: Assignment 1 - Python- Mastering Development in Python - Software Development

1. What are the key concepts of Python development?
Ans. The key concepts of Python development include variables, data types, control structures, functions, and modules. These concepts form the foundation of Python programming and are essential for building applications.
2. How can I improve my Python coding skills?
Ans. To improve your Python coding skills, you can practice regularly, work on coding challenges, participate in coding competitions, read Python programming books, and collaborate with other Python developers on open-source projects.
3. What is the significance of unit testing in Python development?
Ans. Unit testing is crucial in Python development as it helps ensure that individual units of code work correctly. By writing unit tests, developers can verify the functionality of their code, detect bugs early, and maintain code quality throughout the development process.
4. How can I optimize the performance of my Python applications?
Ans. To optimize the performance of your Python applications, you can use profiling tools to identify bottlenecks, optimize algorithms and data structures, leverage concurrency and parallelism, and minimize I/O operations. Additionally, utilizing libraries like NumPy and Cython can improve performance for specific tasks.
5. What are some best practices for Python development?
Ans. Some best practices for Python development include following the PEP 8 style guide, writing clean and readable code, using meaningful variable names, documenting your code, utilizing virtual environments for dependency management, and embracing testing and debugging techniques.
8 videos|5 docs
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Free

,

ppt

,

past year papers

,

Semester Notes

,

video lectures

,

Extra Questions

,

Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development

,

practice quizzes

,

MCQs

,

Exam

,

Sample Paper

,

Viva Questions

,

shortcuts and tricks

,

study material

,

mock tests for examination

,

Important questions

,

Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development

,

pdf

,

Previous Year Questions with Solutions

,

Mastering Development in Python: Assignment 1 | Python- Mastering Development in Python - Software Development

,

Summary

,

Objective type Questions

;