Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Write code to check a String is palindrome or not"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 67,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def palind_check(x):\n",
- " a = x.replace(' ','').lower()\n",
- " if a == a[::-1]:\n",
- " return True\n",
- " else:\n",
- " return False"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 68,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 68,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "palind_check('A Santa dog lived as a devil God at NASA')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 69,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 69,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "palind_check('Nittin')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Write a method which will remove any given character from a String? "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "a = 'this is a %sentence'\n",
- "def remove_character(x,index):\n",
- " return x.replace(x[index],'')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'this is a sentence'"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "remove_character(a,10)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "In an list 1-100 numbers are stored, one number is missing how do you find it?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "75"
- ]
- },
- "execution_count": 54,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "a = list(range(101))\n",
- "a.pop(75)\n",
- "sum(range(101)) - sum(a)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "In an list 1-100 exactly one number is duplicate how do you find it?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "35"
- ]
- },
- "execution_count": 52,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "y = list(range(101))\n",
- "y.append(35)\n",
- "sum(y) - sum (range(101))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Given two list, 1,2,3,4,5 and 2,3,1,0,5 find which number is not present in the second list?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 55,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "a = [1,2,3,4,5]\n",
- "b = [2,3,1,0,5]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 57,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "4\n"
- ]
- }
- ],
- "source": [
- "list(set(a) - set(b)) # Difference a- b will return the value only in a and b -a will return the value only present in b\n",
- "\"\"\" OR\"\"\"\n",
- "\n",
- "for i in a:\n",
- " if i not in b:\n",
- " print (i)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "How do you find second highest number in an integer array?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 58,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "18"
- ]
- },
- "execution_count": 58,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "z = list(range(20))\n",
- "sorted(set(z),reverse = True)[1]\n",
- "\n",
- "\"\"\" OR \"\"\"\n",
- "z.sort(reverse = True)\n",
- "z[1]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "How to swap two numbers without using temp variable?"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 62,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "64 24\n"
- ]
- }
- ],
- "source": [
- "a = 24\n",
- "b = 64\n",
- "a,b = b,a \n",
- "print (a,b)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment