Wednesday 21 February 2018

Zeros and Ones

Zeros and Ones

The zeros tool returns a new array with a given shape and type filled with 's.
import numpy

print numpy.zeros((1,2))                    #Default type is float
#Output : [[ 0.  0.]] 

print numpy.zeros((1,2), dtype = numpy.int) #Type changes to int
#Output : [[0 0]]
The ones tool returns a new array with a given shape and type filled with 's.
import numpy

print numpy.ones((1,2))                    #Default type is float
#Output : [[ 1.  1.]] 

print numpy.ones((1,2), dtype = numpy.int) #Type changes to int
#Output : [[1 1]]   

Task
You are given the shape of the array in the form of space-separated integers, each integer representing the size of different dimensions, your task is to print an array of the given shape and integer type using the tools numpy.zeros and numpy.ones.
Input Format
A single line containing the space-separated integers.
Constraints
Output Format
First, print the array using the numpy.zeros tool and then print the array with the numpy.ones tool.
Sample Input 0
3 3 3
Sample Output 0
[[[0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]]]
[[[1 1 1]
  [1 1 1]
  [1 1 1]]

 [[1 1 1]
  [1 1 1]
  [1 1 1]]

 [[1 1 1]
  [1 1 1]
  [1 1 1]]]
Explanation 0
Print the array built using numpy.zeros and numpy.ones tools and you get the result as shown.
Program:
import numpy
a = []
for x in raw_input().split():
    a.append(int(x))

print numpy.zeros(a, dtype = numpy.int)
print numpy.ones(a, dtype = numpy.int)

Sorted Merge of Two Linked Lists

Given two sorted linked lists, Merge them so that result is also sorted.
Input Format
N - Elements in first list. 
N space separated integers. 
M - Elements in second list. 
M space separated integers.
Constraints
--
Output Format
Single Line of space separated integers.
Sample Input

1 3 7 

2 4 5 9
Sample Output
1 2 3 4 5 7 9
Explanation
Self explanatory.
Program:
Node *sortedMergeLists(Node *h1, Node *h2)
{
    Node *n;
  if(h1==NULL)
    return h2;
  if(h2==NULL)
    return h1;
    if(h1->data < h2->data)
    {
        n=h1;
      h1->next=sortedMergeLists(h1->next,h2);
    }
    else
    {
        n=h2;
        h2->next=sortedMergeLists(h1,h2->next);
    }
  return n;
}

Mindless Algebra

Suppose, 
a = 50 
b = 2000 
c = 40
Define a mathematical function Fun(n) that does the following: 
Fun(n) = n - c, if n > b 
Fun(n) = Fun(a + Fun(a + Fun(a + Fun(a + n)))), if n ≤ b.
Print result of calling this Fun() for n-1, n and n+1.
Input Format
N : an integer N >= 0
Output Format
R1 R2 R3 : 3 integers representing result of function calls - Fun(N-1), Fun(N), Fun(N+1).
Sample Input
2000
Sample Output
2039 2040 1961
Explanation
Well, that's mindless function output for inputs N = 1999, 2000 and 2001.
Program:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int Fun(int n)
{
    if(n>2000)
        return n-40;
    else
        return  Fun(50+ Fun(50 + Fun(50 + Fun(50 + n))));
}
int main() {
    int n;
    cin>>n;
    cout<<Fun(n-1)<<" "<<Fun(n)<<" "<<Fun(n+1);
        
    return 0;
}

FAQs of BCG

These are the some FAQS of BCG PLEASE USE BELOW LINK Details and FAQS of BCG