0% found this document useful (0 votes)
21 views

2d Array

Uploaded by

suchita.arora
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

2d Array

Uploaded by

suchita.arora
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

DSA (WEEK 3-4)

Arrays and Strings


2-Dimensional Array
• Array is a data structure used to store elements. An array can only store similar types
of elements. A Two Dimensional is defined as an Array inside the Array. The index of
the array starts with 0 and ends with a size of array minus 1. We can create ‘n’ number
of arrays in an array.

array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]]
#display
print(array)
Accessing the values
Traversing Elements in 2D list using For loop
Inserting the values into the 2D array
Example
Updating the values into the 2D array
Example
Deleting the values from 2D array
Example
Get the size of 2D array
You can get the size of the two-dimensional array using the len() function. It will return the
number of rows in the array

Example
Programming Exercises

1.Two Sum
Description: Given an array of integers, return indices of the two numbers such that they add
up to a specific target.
Problem Link: Two Sum

2. Best Time to Buy and Sell Stock


Description: Given an array representing stock prices on different days, find the maximum
profit that can be obtained by buying and selling a stock.
Problem Link: Best Time to Buy and Sell Stock

3. Rotate Array
Description: Rotate an array to the right by a given number of steps.
Problem Link: Rotate Array
Programming Exercises

4. Contains Duplicate
Description: Given an array of integers, determine if the array contains any duplicates. Problem
Link: Contains Duplicate

5. Single Number
Description: Given a non-empty array of integers, every element appears twice except for one. Find
that single one.
Problem Link: Single Number

6. Product of Array Except Self


Description: Given an array nums, return an array output where output[i] is equal to the product of
all elements of nums except nums[i].
Problem Link: Product of Array Except Self
Programming Exercises
7. Maximum Subarray
Description: Find the contiguous subarray within an array (containing at least one number) which has
the largest sum.
Problem Link: Maximum Subarray

8. Move Zeroes
Description: Given an array, move all the zeroes to the end while maintaining the relative order of the
other elements.
Problem Link: Move Zeroes

9. Pascal's Triangle
Description: Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
Problem Link: Pascal's Triangle

10. Valid Sudoku Description:


Determine: if a 9x9 Sudoku board is valid. The rules are to fill each row, column, and 3x3 sub-grid
with digits from 1 to 9 without repetition.
Problem Link: Valid Sudoku
Strings
In computer programming, a string is a sequence of characters.
For example, "hello" is a string containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.

We use single quotes or double quotes to represent a string in Python.

For example,

Here, we have created a string variable named string1.


The variable is initialized with the string Python Programming.
Access String Characters in Python
We can access the characters in a string in three ways.

• Indexing: One way is to treat strings as a list and use index values. For example,

• Negative Indexing: Similar to a list, Python allows negative indexing for its strings. For
example,
Access String Characters in Python

Slicing: Access a range of characters in a string by using the slicing operator colon :

For example,
Python Strings are immutable
In Python, strings are immutable. That means the characters of a string cannot be changed.

For example,

However, we can assign the variable name to a new string.


For example,
Python Multiline String
We can also create a multiline string in Python. For this, we use triple double quotes """ or
triple single quotes ‘’’.

For example,
Python String Operations
There are many operations that can be performed with strings which makes it one of the most
used data types in Python.

1. Compare Two Strings


We use the == operator to compare two strings. If two strings are equal, the operator returns True.
Otherwise, it returns False.

For example,
Python String Operations
2. Join Two or More Strings(concatenation)
In Python, we can join (concatenate) two or more strings using the + operator.

For example,

In the above example, we have used the + operator to join two strings: greet and name.
Iterate Through a Python String
• We can iterate through a string using a for loop.
• For example,
Python String Length
• In Python, we use the len() method to find the length of a string.

For example,
String Membership Test
• We can test if a substring exists within a string or not, using the keyword in
Methods of Python String
Methods Description

upper() converts the string to uppercase

lower() converts the string to lowercase

partition() returns a tuple

replace() replaces substring inside

find() returns the index of first occurrence of substring

rstrip() removes trailing characters

split() splits string from left

startswith() checks if string starts with the specified string

isnumeric() checks numeric characters

index() returns index of substring


Python String Formatting (f-Strings)
• Python f-Strings make it really easy to print values and variables.

For example,

Here, f'{name} is from {country}' is an f-string.

This new formatting syntax is powerful and easy to use. From now on, we will use f-Strings
to print strings and variables.
Programming Exercises
1. Valid Palindrome
Description :-Given a string, determine if it is a palindrome, considering only
alphanumeric characters and ignoring cases.
Problem Link: Valid Palindrome

2. Reverse String
Description:-Write a function that reverses a string. The input string is given as an array
of characters.
Problem Link: Reverse String

3. Longest Substring
Description :-Without Repeating Characters Given a string, find the length of the longest
substring without repeating characters.
Problem Link: Longest Substring Without Repeating Characters
Programming Exercises

4. Implement strStr()
Description: Implement the strStr() function to find the first occurrence of a substring in a string. If the
substring is not found, return -1.
Problem Link: Implement strStr()

5. Group Anagrams
Description: Given an array of strings, group anagrams together.
Problem Link: Group Anagrams

6. Valid Parentheses
Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input
string is valid.
Problem Link: Valid Parentheses
Programming Exercises
7. String to Integer (atoi)
Description: Implement the atoi() function which converts a string to an integer. Problem Link:
String to Integer (atoi)

8. Count and Say


Description: The count-and-say sequence is a sequence of strings where each term is generated by
describing the previous term. For example, the first term is "1," the second term is "11," the third term
is "21," and so on.
Problem Link: Count and Say

9. Longest Palindromic Substring


Description: Given a string, find the longest palindromic substring.
Problem Link: Longest Palindromic Substring

10. ZigZag Conversion


Description: Convert the input string into a ZigZag pattern and read it line by line. Problem Link:
ZigZag Conversion

You might also like