<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Md Abdul Hasib</title>
    <description>The latest articles on DEV Community by Md Abdul Hasib (@imsazzad).</description>
    <link>https://dev.to/imsazzad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F287805%2Fc7b55949-51e0-480f-a3d5-c31d2e3a5a61.jpeg</url>
      <title>DEV Community: Md Abdul Hasib</title>
      <link>https://dev.to/imsazzad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imsazzad"/>
    <language>en</language>
    <item>
      <title>FAANG Interview Question| Longest Valid Parentheses</title>
      <dc:creator>Md Abdul Hasib</dc:creator>
      <pubDate>Fri, 22 Apr 2022 08:56:51 +0000</pubDate>
      <link>https://dev.to/imsazzad/google-interview-question-longest-valid-parentheses-47j0</link>
      <guid>https://dev.to/imsazzad/google-interview-question-longest-valid-parentheses-47j0</guid>
      <description>&lt;p&gt;Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = ""
Output: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 &amp;lt;= s.length &amp;lt;= 3 * 104&lt;/li&gt;
&lt;li&gt;s[i] is '(', or ')'.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Solution 1:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# O(n) time | O(n) space - where n is the length of the input string
def longestBalancedSubstring(string):
    stack = [-1]
    longest = 0
    for idx in range(len(string)):
        ch = string[idx]
        if ch == "(":
            stack.append(idx)
        else:
            stack.pop()
            if len(stack) == 0:
                stack.append(idx)
            else:
                top = stack[-1]
                longest = max(longest, idx - top)


    return longest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Better Solution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# O(n) time | O(1) space - where n is the length of the input string
def longestBalancedSubstring(string):
    return max(
            get_longest_sub_string_count(string, True),
            get_longest_sub_string_count(string, False)
        )

def get_longest_sub_string_count(string, left_to_right):
    open_ch= "(" if left_to_right else ")"
    step = 1 if left_to_right else -1
    idx = 0 if left_to_right else len(string) - 1
    open_count = 0
    close_count = 0
    longest = 0
    while idx &amp;gt; -1 and idx &amp;lt; len(string):
        ch = string[idx]
        if ch == open_ch:
            open_count +=1
        else:
            close_count += 1

        if close_count == open_count:
            longest = max(longest, open_count*2)
        elif close_count &amp;gt; open_count:
            open_count = 0
            close_count = 0

        idx += step


    return longest

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>stack</category>
      <category>programming</category>
    </item>
    <item>
      <title>Populate data-frame faster- [from 4 hours to 15 second]</title>
      <dc:creator>Md Abdul Hasib</dc:creator>
      <pubDate>Thu, 16 Sep 2021 04:03:51 +0000</pubDate>
      <link>https://dev.to/imsazzad/populate-data-frame-faster-from-4-hours-to-15-second-557e</link>
      <guid>https://dev.to/imsazzad/populate-data-frame-faster-from-4-hours-to-15-second-557e</guid>
      <description>&lt;p&gt;Hello Guys, So the main problem was on this &lt;a href="https://stackoverflow.com/questions/69127531/how-to-use-pandas-get-dummies-on-prediction-time/69127591#69127591"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To give you a high-level overview. I had to populate a data_frame with about 1300 columns and 1 million rows.&lt;/p&gt;

&lt;p&gt;What I was doing first is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prepare the data frame with zeros for all columns and rows.&lt;/li&gt;
&lt;li&gt;Then, iterating through each column and row and&lt;/li&gt;
&lt;li&gt;assign the value for that cell after some dynamic processing. Here is the code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# main part, not complete code

def populate_data_frame_in_prediction_time(data, columns):
    unknown_col = "nan"
    columns_set = set(columns)
    result_data_frame = pd.DataFrame(0, index=np.arange(len(data)), columns=columns)

    for prefix in data.columns: # O(m)
        unknown_column_name = str(prefix) + "_" + str(unknown_col)
        for index, row in data.iterrows(): #O(n)
            value = row[prefix]
            result_column_name = str(prefix) + "_" + str(value)
            if result_column_name not in columns_set: # O(1)
                result_column_name = unknown_column_name

            result_data_frame[result_column_name][index] = 1

    result_data_frame = result_data_frame.astype('uint8')
    return result_data_frame
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It was a prolonged process. It took about &lt;strong&gt;4+ hours&lt;/strong&gt; for my case of scenery.&lt;br&gt;
Then I have done slightly better but not the best way.&lt;/p&gt;

&lt;p&gt;So what I have in this intermediate approach is &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Did not start work with all columns
Pick a column and make a data frame for it as I converted categorical value to one-hot encoding.&lt;/li&gt;
&lt;li&gt;Then, iterating through each column and row with only that smaller data-frame and&lt;/li&gt;
&lt;li&gt;assign the value for that cell after some dynamic processing.
Repeat the procedure for the next column of the raw data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By doing this, the processing time improves to *&lt;em&gt;20 minutes from 4 hours. *&lt;/em&gt; here is code for each column(series) in the raw data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def _custom_one_hot_encoding_1d(series, column_list):
    unknown_col = "nan"
    prefix = series.name

    number_of_rows, number_of_col = series.shape[0], len(column_list)

    dummy_data = np.array([np.zeros(number_of_rows, dtype=int)] * number_of_col).T
    df = pd.DataFrame(dummy_data, columns=column_list)
    for index, name in series.items():
        if not name:
            name = unknown_col

        column_name = str(prefix) + "_" + str(name)
        if column_name not in df:
            column_name = prefix + "_" + unknown_col
        df[column_name][index] = 1
    return df
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then to improve more, what I did is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;previously, I prepared the data frame first. then populated it.&lt;/li&gt;
&lt;li&gt;I reversed the way.&lt;/li&gt;
&lt;li&gt;I prepared the data first with array[ can be python list / NumPy array)
To do that, I had to write some custom code. It was pandas data-frame earlier.&lt;/li&gt;
&lt;li&gt;after preparing the data, I fill up the data frame with the full data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It improved the performance drastically. Now it is taking only about 15 seconds to do that processing. Here is the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def _custom_one_hot_encoding_1d(series, column_list):
    unknown_col = "nan"
    prefix = series.name

    number_of_rows, number_of_col = series.shape[0], len(column_list)

    column_idx = {column_list[i]: i for i in range(len(column_list))}
    result_arr = np.array([np.zeros(number_of_rows, dtype=int)] * number_of_col).T

    for index, name in series.items():
        if not name:
            name = unknown_col

        column_name = str(prefix) + "_" + str(name)
        if column_name not in column_list:
            column_name = prefix + "_" + unknown_col
        result_arr[index][column_idx[column_name]] = 1

    df = pd.DataFrame(result_arr, columns=column_list)
    return df

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is my story. Thank you guys for reading. If you guys have any better idea, please suggest here. I will be happy to try that in my code.&lt;/p&gt;

</description>
      <category>python</category>
      <category>machinelearning</category>
      <category>datascience</category>
      <category>pandas</category>
    </item>
    <item>
      <title>How to Publish and Use Your Python Package in  and from Private Bitbucket or Github </title>
      <dc:creator>Md Abdul Hasib</dc:creator>
      <pubDate>Thu, 04 Mar 2021 10:14:29 +0000</pubDate>
      <link>https://dev.to/imsazzad/how-to-publish-and-use-your-python-package-in-and-from-private-bitbucket-or-github-3p4c</link>
      <guid>https://dev.to/imsazzad/how-to-publish-and-use-your-python-package-in-and-from-private-bitbucket-or-github-3p4c</guid>
      <description>&lt;p&gt;Python has become one of the most popular programming languages. One major reason is that we, regular Python users, are free to share our code and others can use it very conveniently. A formal way of such code sharing is to pack all your code into a package and upload it to the Python Package Index (pypi.org), through which other Python users can install your package easily using the pip tool.&lt;/p&gt;

&lt;p&gt;If you have published a Python package yourself, you should know that the process is not difficult. However, for those who have never done it, you may have mistakenly thought that it must be a painful process. &lt;/p&gt;

&lt;p&gt;In this article, I’ll show you the steps to preparing your package and publishing it.&lt;br&gt;
But I will do it by uploading to bitbucket, not in PyPI.org. Because If you are doing it for a company they may not want to upload it for public use. So let's get into it.&lt;/p&gt;

&lt;p&gt;Apparently, the first step is to complete your project. However, we all understand that packages are never perfect. As a result, you don’t have to wait for the completion of your project before you can figure out how to publish your package. The setup file&lt;br&gt;
As mentioned previously, we use the setup.py file to indicate how our package should be prepared. The following code shows you what a typical setup.py file looks like:&lt;/p&gt;

&lt;p&gt;For the purpose of the current tutorial, let’s suppose that the directory that has all your Python files (e.g. &lt;strong&gt;file_1.py&lt;/strong&gt;, as shown later) is called &lt;strong&gt;you_package&lt;/strong&gt;. This is the one that you want to publish. To keep things simple, the file &lt;strong&gt;file_1.py&lt;/strong&gt; has just the following code, which will be used when we’re ready to import the package after its publication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def hello_world():
    print("Hello World!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  1. Prepare Files
&lt;/h2&gt;

&lt;p&gt;Before we dive into the details for publishing, let’s first structure the directory with the necessary support files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;the_root_dir/
|-- README.md
|-- setup.py
|-- your_package
|--|-- __init__.py
|--|-- file_0.py
|--|-- file_1.py
|--|-- file_2.py

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;At the root directory, at the same level as the your_package, create the README.md file. This file will cover things that you want the users to know about your package, such as installation and use instructions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Another file at the same level is the setup.py file. This file will contain the information necessary to set up the package for publishing. We’ll talk about what’s inside this file in a minute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Within the your_package directory, create the &lt;strong&gt;init&lt;/strong&gt;.py file. This file has to be named such, and what it does is convert a regular directory to a Python package. For the current simple package, we can just put the code in the file_1.py for exporting &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;from you_package.file_0 import hello_world&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The README file
&lt;/h2&gt;

&lt;p&gt;Theoretically, you can use just plain text for this file, but I prefer to use a markdown file that gives you formatting options to make it more interesting to read. &lt;br&gt;
Please note that the gist is saved as a TXT file, which shows you the source text for the markdown before bitbucket automatically applies the formatting for markdown files.&lt;br&gt;
Usually, it’s recommended that you include the license information about the package so that the users can know the terms. &lt;/p&gt;
&lt;h2&gt;
  
  
  The setup file
&lt;/h2&gt;

&lt;p&gt;As mentioned previously, we use the 'setup.py' file to indicate how our package should be prepared. The following code shows you what a typical 'setup.py' file looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import setuptools

with open("README.md") as file:
    read_me_description = file.read()

setuptools.setup(
    name="your-package-username",
    version="0.1",
    author="Your Name",
    author_email="your_email",
    description="This is a test package.",
    long_description=read_me_description,
    long_description_content_type="text/markdown",
    url="package_bitbucket_page",
    packages=['your_package'],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='&amp;gt;=3.7',
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We first import the &lt;strong&gt;setup tools&lt;/strong&gt; module, which has convenient methods for packaging setup.&lt;/li&gt;
&lt;li&gt;In the setup() function call, most parameters are very straightforward and should be self-explanatory. Some highlights are shown below:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;name: The distribution name of the package, which has to be unique on pypi.org. To ensure uniqueness, it’s always a good idea that you append your PyPI username to your package name if you want to upload it in Pypi also.&lt;/p&gt;

&lt;p&gt;long_description: The long description is usually set from the README.md file. To show the description properly, you specify that it is a markdown file by setting the long_description_content_type parameter.&lt;/p&gt;

&lt;p&gt;packages: The list of packages that you want to publish in the distribution package. Because we’re only publishing just one package (your_package), its name is shown in a list. However, if you want to publish all packages in the directory, you can use setup tools.find_packages() to retrieve them conveniently.&lt;/p&gt;

&lt;p&gt;python_requires: Specifies the version of Python that your package requires.&lt;/p&gt;

&lt;p&gt;When you use it from another codebase pip will take the version name you mentioned in the setup.py file.&lt;/p&gt;

&lt;p&gt;Now I am assuming you have committed and pushed this code into Bitbucket. Now it is time to use it from another codebase.&lt;/p&gt;

&lt;p&gt;from the root directory, you want to use this library&lt;br&gt;
&lt;code&gt;pip install git+https://user_name@bitbucket.org/company_name/your_project.git#egg=name_in_setup_file_you_wrote&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or if you want to download a specific tagged version &lt;br&gt;
&lt;code&gt;pip install git+https://user_name@bitbucket.org/company_name/your_project.git@1.0.7#egg=name_in_setup_file_you_wrote&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will download the version you have in the setup.py file, actually the latest version.&lt;/p&gt;

&lt;p&gt;If you want to download a specific version tagged in 'git tag', you can tag that commit using git and update that version in the setup.py file also. Because pip does not recognize git tag. Pip download that Commit you have tagged and treat the version written in setup.py as the library version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we learned the major steps that we use to create a Python package for distribution through bitbucket. &lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pip.pypa.io/en/stable/reference/pip_install/"&gt;https://pip.pypa.io/en/stable/reference/pip_install/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pythonhosted.org/an_example_pypi_project/setuptools.html"&gt;https://pythonhosted.org/an_example_pypi_project/setuptools.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://packaging.python.org/tutorials/packaging-projects/"&gt;https://packaging.python.org/tutorials/packaging-projects/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/book/en/v2/Git-Basics-Tagging"&gt;https://git-scm.com/book/en/v2/Git-Basics-Tagging&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
