Found 33676 Articles for Programming

Convert string to DateTime and vice-versa in Python

Pradeep Elance
Updated on 04-Jun-2020 11:13:41

255 Views

Python has extensive date and time manipulation capabilities.In this article we'll see in how is string with proper format can we converted to a datetime and the vice versa.With strptimeThis strptime function from datetime module can do the conversion from string to datetime by taking appropriate format specifiers.Example Live Demoimport datetime dt_str = 'September 19 2019 21:02:23 PM' #Given date time print("Given date time: ", dt_str) #Type check print("Data Type: ", type(dt_str)) #Format dtformat = '%B %d %Y %H:%M:%S %p' datetime_val = datetime.datetime.strptime(dt_str, dtformat) print("After converting to date time: ", datetime_val) #Type check print("Data type: ", type(datetime_val)) # Reverting to string ... Read More

Convert string enclosed list to list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:11:47

640 Views

We may sometime get data which contains strings but the structure of the data inside the stream is a Python list. In this article we will convert string enclosed list to an actual Python list which can be further used in data manipulation.With evalWe know the eval function will give us the actual result which is supplied to it as parameter. So so we supplied the given string to the eval function and get back the Python list.Example Live DemostringA = "['Mon', 2, 'Tue', 4, 'Wed', 3]" # Given string print("Given string : ", stringA) # Type check print(type(stringA)) # using ... Read More

Shortest Common Supersequence in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:14:39

207 Views

Suppose we have two strings str1 and str2, we have to find the shortest string that has both str1 and str2 as subsequences. There may be more than one results, so we will return only one of them.As you know a string S is called a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywhere from T) results in the string S.So, if the input is like "acab", "bac", then the output will be "bacab", this is because two given strings are subsequence of this.To solve this, we will ... Read More

Number of Submatrices That Sum to Target in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:09:58

230 Views

Suppose we have a matrix, and a target value, we have to find the number of non-empty submatrices that sum is same as target. Here a submatrix [(x1, y1), (x2, y2)] is the set of all cells matrix[x][y] with x in range x1 and x2 and y in range y1 and y2. Two submatrices [(x1, y1), (x2, y2)] and [(x1', y1'), (x2', y2')] are different if they have some coordinate that is different: like, if x1 is not same as x1'.So, if the input is like010111010and target = 0, then the output will be 4, this is because four 1x1 ... Read More

Longest Duplicate Substring in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:06:27

660 Views

Suppose we have a string S, consider all duplicated contiguous substrings that occur 2 or more times. (The occurrences may overlap.), We have to find the duplicated substring that has the longest possible length. If there is no such substrings, then return a blank string. As the answer may very large, so return in mod 10^9 + 7.So, if the input is like "ababbaba", then the output will be "bab"To solve this, we will follow these steps −m := 1e9 + 7Define a function add(), this will take a, b, return ((a mod m) + (b mod m)) mod mDefine ... Read More

Escape a Large Maze Python

Arnab Chakraborty
Updated on 04-Jun-2020 11:00:48

434 Views

Suppose we have a grid, there are 1 million rows and 1 million columns, we also have one list of blocked cells. Now we will start at the source square and want to reach the target square. In each move, we can walk to a up, down, left, right adjacent square in the grid that isn't in the given list of blocked cells.We have to check whether it is possible to reach the target square through a sequence of moves or not.So, if the input is like blocked = [[0, 1], [1, 0]], source = [0, 0], target = [0, ... Read More

Stream of Characters in C++

Arnab Chakraborty
Updated on 04-Jun-2020 10:58:38

606 Views

Suppose we want to implement the StreamChecker class as follows −StreamChecker(words) − This is the constructor, this initializes the data structure with the given words.query(letter) − This returns true when for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.So, if the input is like word list = ["ce", "g", "lm"], then call query many times for [a, b, c, e, f, g, h, i, j, k, l, m], then the output will be true for e, g, m, and ... Read More

Numbers With Repeated Digits in C++

Arnab Chakraborty
Updated on 04-Jun-2020 10:53:46

685 Views

Suppose we have a positive integer N, we have to find the number of positive integers less than or equal to N that have at least 1 repeated digit .So, if the input is like 99, then the output will be 9, as we have numbers like 11, 22, 33, 44, 55, 66, 77, 88, 99.To solve this, we will follow these steps −Define a function A(), this will take m, n, ret := 1for initialize i := 0, when i < n, update (increase i by 1), do −ret := ret * m(decrease m by 1)return retFrom the main ... Read More

Grid Illumination in Python

Arnab Chakraborty
Updated on 04-Jun-2020 10:49:36

456 Views

Suppose we have a N x N grid of cells, in each cell (x, y) there is a lamp. Initially, some of the lamps are on. The lamps[i] is the location of the i-th lamp that is on. Each lamp that is on glows every square on its x-axis, y-axis, and both diagonals. Now for the i-th query i.e. queries[i] = (x, y), the answer to the query is 1 if the cell (x, y) is glowed, otherwise 0. After each query (x, y), we turn off any lamps that are at cell (x, y) or are adjacent 8-directionally. Return ... Read More

Minimum Cost to Merge Stones in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:52:30

638 Views

Suppose we have N piles of stones arranged in a row. Here the i-th pile has stones[i] number of stones. A move consists of merging K consecutive piles into one pile, now the cost of this move is equal to the total number of stones in these K number of piles. We have to find the minimum cost to merge all piles of stones into one pile. If there is no such solution then, return -1.So, if the input is like [3, 2, 4, 1] and K = 2, then the output will be 20, this is because, we will ... Read More

Advertisements