PYTHON Language
// Odd-Even- Difference
def OddEvenDifference(input1, input2):
c, d=0,0
for i in input2:
if i%2==0:
C+=i else:
d+=i
return d-c
// Find-Total-Feet
def findTotalFeet (input1, input2):
c=0
for i in input2:
C+=i//12
return c
_________________
// Find-Total-Distance
def findTotalDistance (input1, input2): 1=[]
for i in range (len (input2)-1):
1.append(abs (input2[i]-input2 [i+1]))
return sum(1)
________________
// Find-Total-Cost
def findTotalCost (input1,input2):
for i in input2:
if(i<=1000):
C+=0
else:
c+=(1-1000)//100*10
return c
______________________
// Find-Total-Feet
def findTotalfeet c=0
(input1, input2):
for i in input2: C+=1//12
return c
__________________
//Find-Small-Large-Difference
def findsmallLargeDifference (inputi, input2):
return max(input2)-min(input2)
____________________________
// Find-Max-Difference
def findMaxDifference (input1, input2):
1=[] for i in range (len (input2)-1):
1.append(1[1]-1[i+1])
return max (1)
// findMaxDifference
def findMaxDifference(n,numbers):
maximum = 0
for i in range(n-1):
if numbers[i] - numbers[i+1] > maximum:
maximum = numbers[i] - numbers[i+1]
return maximum
n = int(input())
numbers = list(map(int, input().split()))
print(findMaxDifference(n,numbers))
___________________
// findOddEvenDifference
int findOddEvenDifference(int input1, int input2[])
{
int odd= 0, even = 0;
for(int i=0; i<input1; i++)
{
if (input2[i] & 1)
odd += input2[i];
else
even += input2[i];
}
return odd-even;
}
__________________________
// findOddEvenDifference
#include<iostream>
using namespace std;
int findOddEvenDifference(int n, int arr[])
{
int odd=0,even=0;
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
even+=arr[i];
else
odd+=arr[i];
}
return odd-even;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)cin>>arr[i];
cout<<findOddEvenDifference(n,arr);
return 0;
}
_________________________
// OddEvenDifference
def OddEvenDifference(input1,input2):
c,d=0,0
for i in input2:
if i%2==0:
c+=i
else:
d+=i
return d-c
_____________________
// findTotalCost
def findTotalCost(input1,input2):
c=0
for i in input2:
if(i<=1000):
c+=0
else:
c+=(i-1000)//100*10
return c
_____________
Solution of the question- "ben loves grid"
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
int m,n,k;
const int maxn = 500+5;
char G[maxn][maxn];//store matrix information
int vis[maxn][maxn];//Access ID
//dfs direction
int dr[4]={1,-1,0,0};
int dc[4]={0,0,-1,1};
int sumOfEcell;//The number of remaining spaces
int sumOfPoint;//The number of connection points
//Determine whether it is out of bounds
bool isCan(int r,int c){
if(1<=r&&r<=m&&1<=c&&c<=n) return true;
return false;
}
//Print the result
void print(){
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++)
cout<<G[i][j];
cout<<endl;
}
}
//Update matrix
//If you find the answer, change the remaining spaces to'X', otherwise zero the access flags of all spaces
void Updata(int flag){
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++){
if(flag){
if(!vis[i][j]) G[i][j]='X';
}
else if(G[i][j]=='.') vis[i][j]=0;
}
}
bool dfs(int r,int c){
vis[r][c]=1;
sumOfPoint++;//Add one for every space
if(sumOfPoint==sumOfEcell) return true;
for(int i=0;i<4;i++){
if(!vis[r+dr[i]][c+dc[i]]&&isCan(r+dr[i],c+dc[i])&&G[r+dr[i]][c+dc[i]]=='.')
if(dfs(r+dr[i],c+dc[i])) return true;//Find the answer
}
return false;
}
void solve(){
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
if(!vis[i][j]){
sumOfPoint=0;
if(dfs(i,j)){
Updata(1);
print();
return;//Find the solution and exit
}
else Updata(0);
}
}
int main(){
mem(G);mem(vis);
while(cin>>m>>n>>k){
sumOfEcell=m*n-k;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++){
cin>>G[i][j];
if(G[i][j]=='#'){
vis[i][j]=1;
sumOfEcell--;
}
}
solve();
}
}
______________
// FindMaxDiff
m=0
for i in range(input1-1):
a=input2[i]-input2[i+1]
m=max(m,a)
return m
Python3
________________
// finalSum
int finalSum(int Arr[],int N)
{
int ans = 0;
for(int i = 0;i<N;i++)
{
int tmp,n;
tmp = n = Arr[i];
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
if(sum==K)
ans+=tmp;
}
return ans;
}
________________
// findCommonElements
Result findCommonElements(int input1[], int input2[], int input3[], int input4, int input5, int input6){
map<int, int> m1, m2, m3;
for(int i = 0; i < input4; i++)
m1[input1[i]] += 1;
for(int i = 0; i < input5; i++)
m2[input2[i]] += 1;
for(int i = 0; i < input6; i++)
m3[input3[i]] += 1;
Result ans;
int k = 0;
for(auto it: m1){
int num = it.first;
if(m1[num] > 0 && m2[num] > 0 && m3[num] > 0)
ans.output1[k++] = num;
}
return ans;
}
_________________
// Reverse Code
function reverse(input1, input2) {
var res = new Array;
for(var i = input2-1; i >= 0; i--) {
res.push(input1[i]);
}
return res;
}
_____________
// maximumChars
def maximumChars(str1):
n = len(str1)
res = -1
# Initialize all indexes as -1.
firstInd = [-1 for i in range(MAX_CHAR)]
for i in range(n):
first_ind = firstInd[ord(str1[i])]
# If this is first occurrence
if (first_ind == -1):
firstInd[ord(str1[i])] = i
# Else find distance from previous
# occurrence and update result (if
# required).
else:
res = max(res, abs(i - first_ind - 1))
return res
_______________
// pushZeroesEnd
struct Result pushZeroesEnd(int input1[], int input2){
Result* ans = new Result();
int k = 0, count = 0;
for(int i = 0; i < input2; i++){
if(input1[i] == 0)
count += 1;
else
ans->output1[k++] = input1[i];
}
while(count--)
ans->output1[k++] = 0;
return *ans;
};
________________
// Remove Duplicates
ans=""
for i in input1:
if i not in ans:
ans+=i
return ans
Python
____________________
// maximumChars
def maximumChars(str1):
n = len(str1)
res = -1
# Initialize all indexes as -1.
firstInd = [-1 for i in range(MAX_CHAR)]
for i in range(n):
first_ind = firstInd[ord(str1[i])]
# If this is first occurrence
if (first_ind == -1):
firstInd[ord(str1[i])] = i
# Else find distance from previous
# occurrence and update result (if
# required).
else:
res = max(res, abs(i - first_ind - 1))
return res
Happy String
_______________
#include<bits/stdc++.h>
using namespace std;
char* removeDuplicates(char* input1){
map<char, int> m1;
string ans;
int n = strlen(input1);
cout << n << endl;
for(int i = 0; i < n; i++){
if(m1.find(input1[i]) == m1.end())
ans += input1[i];
m1[input1[i]] = 1;
}
int len = ans.length();
char res[len + 1];
strcpy(res, ans.c_str());
return res;
}
______________
// Reverse an array
for(int i=input2-1;i>=0;i--)
{
System.out.print(input1[i]);
}
_______________
Comments
Post a Comment