14,241,236 members
Rate this:
See more:
```def readinput():
n = int(input())     # Length
for j in range(n):
nextnum = int(input())  # Read each value
insequence.append(nextnum)
best.append(0)     # Initialize best[k] for each position
return

def solve():
for j in range(len(insequence)):
# Collect best[k] for values to left of j that divide insequence[j]
prev = [ best[k] for k in range(j) if insequence[j]%insequence[k] == 0 ]
if prev:
best[j] = 1 + max(prev)
else:
best[j] = 1

insequence = []
best = []
solve()
print(max(best))```

What I have tried:

Posted
Updated 17-Oct-18 10:54am
v4
Patrice T 17-Oct-18 15:27pm

What about learning Python to understand this code, then C to write the translation?

Rate this:

## Solution 2

C++ is quite expressive:
```#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

{
size_t n;
cin >> n;
vector<int> v(n);
for (auto & x : v)
cin >> x;
return v;
}
vector <int> solve(const vector<int> & inseq)
{
auto size = inseq.size();
vector<int> best(size);
for (size_t j=0; j<size; ++j)
{
vector<int> prev;
for (size_t k=0; k<j; ++k)
if ( inseq[j] % inseq[k] == 0)
prev.push_back(best[k]);
best[j] = prev.size() > 0 ? 1 + *max_element(prev.begin(), prev.end()) : 1;
}
return best;
}

int main()
{
auto inseq = readinput();
auto best = solve(inseq);
cout << *max_element(best.begin(), best.end()) << endl;
}```
Rate this:

## Solution 1

Forget it. Python is too different from C to make any conversion simple. Many of the features of Python have no direct equivalent in C so you would need to design your own alternatives. Just use the design details of the program to write it in C.
CPallini 17-Oct-18 16:55pm

Maybe difficult to implement using `C`. On the other hand, `C++`...
Richard MacCutchan 18-Oct-18 4:02am

That's easy for you to say ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 133 phil.o 110 Afzaal Ahmad Zeeshan 90 Richard Deeming 80 RickZeeland 79
 OriginalGriff 4,714 Richard MacCutchan 1,348 Maciej Los 1,115 CPallini 904 MadMyche 879

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100